如何将grepl应用于数据框

时间:2017-05-12 11:12:49

标签: r nlp text-mining text-analysis

解决方案:

@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()中有多个模式。

所需的输出是:

                                 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 个答案:

没有答案