【问题标题】:Count occurrences of specific words from a dataframe row in R计算 R 中数据框行中特定单词的出现次数
【发布时间】:2014-08-24 08:42:24
【问题描述】:

我有一个包含 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')

【问题讨论】:

  • 在这种情况下匹配应该为零,对吧?查找 gregexprregmatches 作为起点。或者,有一些软件包可以使用,例如“stringr”或“stringi”。
  • 欢迎来到 StackOverflow!请阅读有关how to ask a good question 的信息以及如何生成minimal reproducible example。这将使其他人更容易帮助您。

标签: r string count sum word-count


【解决方案1】:

我认为这是您需要的

样本数据

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

【讨论】:

  • 这工作得很好,但仍然存在问题,因为我不是在搜索一个特定的词,而是一个表达式,如果我用肯定它工作。但是如果我想用表达式 \n Positive\n 来做它不会。
  • 你能用更好的样本更新问题吗?我刚刚尝试使用“\n Positive”,它给了我正确的计数。
  • 我更新了一个更好的示例并根据您的解决方案发布了代码,但在我的情况下它不起作用。只有当我搜索正面、中性和负面时它才有效。
【解决方案2】:

为了提供一些替代方案,让我们从 @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

【讨论】:

    【解决方案3】:

    正如 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]) 进行投射。

    【讨论】:

      【解决方案4】:

      为什么不只是:

      dataset$Positiv <- str_count(dataset$Content, 'Positiv')
      dataset$Neutral <- str_count(dataset$Content, 'Neutral')
      dataset$Negativ <- str_count(dataset$Content, 'Negativ')
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-11-17
        • 2014-08-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-09
        相关资源
        最近更新 更多