2017-05-12 28 views
0

SOLUTION:如何申請grepl數據幀

發佈者@docendodiscimus:「嘗試的sapply環比模式:

sapply(df_triggers$trigger, grepl, df_sen$sentence)" 

最初的問題:

我想用grepl用於定義爲數據幀的多個模式 df_sen表示爲

sentence 
"She would like to go there" 
"I had it few days ago" 
"We have spent few millions" 

個df_triggers現分述如下:

trigger 
few days 
few millions 

我想創建一個矩陣,其中一句X觸發器和在路口看到1,如果觸發是在一個句子中發現和0,如果事實並非如此。

我試圖做這樣的:

df_sen = read.csv("df.csv", sep = ";",header = TRUE) 
df_triggers = read.csv("df.csv", sep = ";",header = TRUE) 
dat <- data.frame(v=df_sen$sentence) 
matrix <- grepl(df_triggers$trigger, dat$v) 
write.csv(matrix,file = "matrix.csv") 

但我看到,我在grepl超過1種模式()的錯誤信息。

所需的輸出是:

        few days few millions 
"She would like to go there"  0    0 
"I had it few days ago"    1    0 
"We have spent few millions   0    1 

你能幫我達成這樣的結果嗎?

+0

而事情是,我有2500個句子。所以我不能像'幾天|幾百萬' – ZverArt

+3

嘗試一個模式的sapply循環:'sapply(df_triggers $ trigger,grepl,df_sen $句子)' –

+0

@docendodiscimus謝謝!它運作良好! – ZverArt

回答

2
sapply(df_triggers$trigger, grepl, df_sen$sentence) 

從@docendodiscimus工作。