计算R中数据帧行中特定单词的出现次数

时间:2014-07-03 09:34:22

标签: r string count sum word-count

我有一个包含2列和多行的数据集。 第一列ID,第二列是属于它的文本。

我想添加更多列,这些列总结了某行字符串在行中显示的次数。字符串将是“\ 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

现在我尝试了这样,但它没有给出正确的答案:

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

4 个答案:

答案 0 :(得分: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

答案 1 :(得分: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

答案 2 :(得分:0)

如Ananda所指出的,这也可以在不加载任何其他库的情况下完成。我的解决方案是,如果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])进行投射。

答案 3 :(得分:0)

为什么不只是:

dataset$Positiv <- str_count(dataset$Content, 'Positiv')
dataset$Neutral <- str_count(dataset$Content, 'Neutral')
dataset$Negativ <- str_count(dataset$Content, 'Negativ')
相关问题