Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
计算单词在每行中出现并存储在新列中的次数(dplyr)_R_Dplyr_Text Mining_Word Frequency_Qdap - Fatal编程技术网

计算单词在每行中出现并存储在新列中的次数(dplyr)

计算单词在每行中出现并存储在新列中的次数(dplyr),r,dplyr,text-mining,word-frequency,qdap,R,Dplyr,Text Mining,Word Frequency,Qdap,我有一个基本上包含单词段落的字符向量。我想分别计算一个特定单词在每一行中出现的次数,然后创建一个新的向量来保存这个数字。如何使用dplyr实现这一点?(任何其他可用的方法也可以) 我找到的最接近的解决方案是在这个链接上:但它并没有给我我想要的东西。您可以按分隔符分割段落,并对条件检查求和: df <- structure(list(words = c("CDjointdisease state glasses CDdiabetes eyesight",

我有一个基本上包含单词段落的字符向量。我想分别计算一个特定单词在每一行中出现的次数,然后创建一个新的向量来保存这个数字。如何使用dplyr实现这一点?(任何其他可用的方法也可以)


我找到的最接近的解决方案是在这个链接上:但它并没有给我我想要的东西。

您可以按分隔符分割段落,并对条件检查求和:

df <- structure(list(words = c("CDjointdisease state glasses CDdiabetes eyesight", 
                               "accidents_combined docvisits4w citysize CDliverdisease CDosteoporosis"
)), .Names = c("words"), row.names = 1:2, class = "data.frame")


> df
                                                                      words
1                          CDjointdisease state glasses CDdiabetes eyesight
2     accidents_combined docvisits4w citysize CDliverdisease CDosteoporosis

df$count <- sapply(strsplit(df$words, " "), function(x){
  sum(x == "eyesight")
})

> df
                                                                      words count
1                          CDjointdisease state glasses CDdiabetes eyesight     1
2     accidents_combined docvisits4w citysize CDliverdisease CDosteoporosis     0
df
话
1 CDjointdisease state眼镜cddm视力
2次事故\u联合文件访问4W城市化疾病骨质疏松症
df$count df
字数很重要
1 CDjointdisease state眼镜CDdiabetes视力1
2起事故\u联合文件访问4W城市化CD0

欢迎来到SO!请向我们提供您数据的代表性部分。这将允许有人谁想要帮助你的东西玩具和实验。