2014-07-03 221 views
0

我有一个包含2列和多行的数据集。 第一列ID,第二列属于它的文本。计算R中某个数据帧行的特定词的出现次数

我想添加更多的列,总结某些字符串在行的文本中出现的次数。该字符串将是 “\ n个正\ n”, “\ N零\ n”, “\ n是负面的\ 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 

现在,我尝试过这样的,但它没有做出正确的回答:

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') 
+0

而在这种情况下,比赛应该是零,对吧?查找'gregexpr'和'regmatches'作为起点。或者,有几个软件包可以像“stringr”或“stringi”一样使用。 – A5C1D2H2I1M1N2O1R2T1

+0

欢迎来到StackOverflow!请阅读关于[如何提出一个好问题](http://stackoverflow.com/help/how-to-ask)以及如何生成[最小可重现示例]的信息(http://stackoverflow.com/questions/5963269 /如何对化妆一个伟大-R-重复性,例如/ 5963610#5963610)。这会让其他人更容易帮助你。 – Jaap

回答

2

我假定这就是你需要

的样本数据

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将应该给更新的数据集

> 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 
+0

这工作还算不错,但仍然存在问题,因为我不是在搜索特定的单词,而是在表达式中搜索,如果我将它与“正面”结合使用。但是,如果我想用表达式来做\ n正面\ n它不会。 – Carlo

+0

可以用更好的样本更新问题吗?我只是尝试'\ n正面',它给了我适当的计数。 –

+0

我更新了一个更好的示例,并根据您的解决方案发布了代码,但在我的情况下,它不起作用。它只适用于我搜索正面,中性和负面。 – Carlo

1

提供一些选择,让我们开始略加修改@ on_the_shores_of_linux_sea的数据集。

id <- c(1:4) 
text <- c('I have a Dataset with 2 columns a', 
      'nd multiple rows. first column ID rows', 
      'second column the text which', 
      'n the text which belongs to it.') 
dataset <- data.frame(id,text) 

用基本的R功能粘贴,你可以想出一个像这样的功能。

wordCounter <- function(invec, word, ...) { 
    vapply(regmatches(invec, gregexpr(word, invec, ...)), length, 1L) 
} 

你会使用这样的:

## allows other arguments to gregexpr 
wordCounter(dataset$text, "id", ignore.case = TRUE) 
# [1] 0 1 0 0 
wordCounter(dataset$text, "id") 
# [1] 0 0 0 0 
wordCounter(dataset$text, "rows") 
# [1] 0 2 0 0 
wordCounter(dataset$text, "second", ignore.case = TRUE) 
# [1] 0 0 1 0 

另一种选择,如果你想要去一些现成的解决方案,将使用“stringi”包,里面有一个漂亮的stri_count*功能集。在这里,我用stri_count_fixed

library(stringi) 
stri_count_fixed(dataset$text, "rows") 
# [1] 0 2 0 0 
0

这也可以不加载任何额外的库,由阿南达指出。我的解决办法是,提供了2列的表被称为dataset并查找字符串是mystring

countOccurr = function(text,motif) { 
res = gregexpr(motif,text,fixed=T)[[1]] 
ifelse(res[1] == -1, 0, length(res)) 
} 

dataset = cbind(dataset, count = vapply(dataset[,2], countOccurr, 1, motif=mystring)) 

当心,你的数据框的第二列必须是模式字符,如果你想避免问题(@ on-the-shores-of-linux-sea作为示例数据给出的数据框保留了模式因子,这对他的解决方案来说很好,但与我的解决方案无关)。否则使用as.character(dataset[,2])进行施放。

相关问题