计算R中数据框行中特定单词的出现次数 [英] Count occurrences of specific words from a dataframe row in R
本文介绍了计算R中数据框行中特定单词的出现次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个2列多行的数据集。
第一列ID,第二列是其所属的文本。
I have a Dataset with 2 columns and multiple rows. first column ID, second column the text which belongs to it.
我想添加更多列,以总结某个字符串出现在列表中的次数。行中的文本。字符串将是 \n Positive\n, \n Neutral\n, \n Negativ\n。
I want to add more columns which sums up how many times a certain string appears in the text from the Row. the string would be "\n Positive\n", "\n Neutral\n", "\n Negativ\n"`
示例数据集:
Id, Content
2356, I like cheese.\n Positive\nI don't want to be here.\n Negative\n
3456, I am alone.\n Neutral\n
最后应该看起来像
Id, Content,Positiv, Neutral, Negativ
2356, I like cheese.\n Positive\nI don't want to be here.\n Negative\n,1 ,0 ,1
3456, I am alone.\n Neutral\n, 0, 1, 0
现在我像这样尝试过,但这不是t给出正确答案:
Right now i tried it like this but it isn't giving the right answers:
getCount1 <- function(data, keyword)
{
Positive <- str_count(Dataset$CONTENT, keyword)
return(data.frame(data,Positive))
}
Stufe1 <-getCount1(Dataset,'\n Positive\n')
################################################################
getCount2 <- function(data, keyword)
{
Neutral <- str_count(Stufe1$CONTENT, keyword)
return(data.frame(data,Neutral))
}
Stufe2 <-getCount2(Stufe1,'\n Neutral\n')
#####################################################
getCount3 <- function(data, keyword)
{
Negative <- str_count(Stufe2$CONTENT, keyword)
return(data.frame(data,Negative))
}
Stufe3 <-getCount3(Stufe2,'\n Negative\n')
推荐答案
我假设这是您所需要的
样本数据
id <- c(1:4)
text <- c('I have a Dataset with 2 columns a',
'nd multiple rows. first column ID', 'second column the text which',
'n the text which belongs to it.')
dataset <- data.frame(id,text)
查找计数的功能
library(stringr)
getCount <- function(data,keyword)
{
wcount <- str_count(dataset$text, keyword)
return(data.frame(data,wcount))
}
调用getCount应该给出更新的数据集
Calling getCount should give the updated dataset
> getCount(dataset,'second')
id text wcount
1 I have a Dataset with 2 columns a 0
2 nd multiple rows. first column ID 0
3 second column the text which 1
4 n the text which belongs to it. 0
这篇关于计算R中数据框行中特定单词的出现次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文