【问题标题】:How do I count the number of words from a list mentioned in a data frame in R如何计算R中数据框中提到的列表中的单词数
【发布时间】:2019-12-19 03:31:34
【问题描述】:

我有一个包含多行评论和文本列的数据框。我还有一个包含单词的列表。我想要一个 for 循环来检查数据帧的每一行,以总结从列表中找到的单词数。我想保持每行总和由行分隔并将结果放入新的结果数据框中。

#Data Frame
Review           Text
1           I like to run and play.
2           I eat cookies.
3           I went to swim in the pool.
4           I like to sleep.
5           I like to run, play, swim, and eat.

#List Words
Run
Play
Eat
Swim

#Result Data Frame
Review      Count
1            2
2            1
3            1
4            0
5            4

【问题讨论】:

    标签: r dataframe count apply


    【解决方案1】:

    这是base R 的解决方案,其中gregexpr 用于计算出现次数。

    给定如下模式

    pat <- c("Run", "Play", "Eat", "Swim")
    

    那么添加到数据框的计数可以通过以下方式进行:

    df$Count <- sapply(gregexpr(paste0(tolower(pat),collapse = "|"),tolower(df$Text)), 
                       function(v) ifelse(-1 %in% v, 0,length(v)))
    

    这样

    > df
      Review                                Text Count
    1      1              I like to run and play     2
    2      2                       I eat cookies     1
    3      3         I went to swim in the pool.     1
    4      4                    I like to sleep.     0
    5      5 I like to run, play, swim, and eat.     4
    

    【讨论】:

      【解决方案2】:

      我们可以在将words 粘贴在一起作为一个模式之后使用stringr::str_count

      df$Count <- stringr::str_count(df$Text, 
                         paste0("\\b", tolower(words), "\\b", collapse = "|"))
      
      df
      #  Review                                Text Count
      #1      1             I like to run and play.     2
      #2      2                      I eat cookies.     1
      #3      3         I went to swim in the pool.     1
      #4      4                    I like to sleep.     0
      #5      5 I like to run, play, swim, and eat.     4
      

      数据

      df <- structure(list(Review = 1:5, Text = structure(c(2L, 1L, 5L, 4L, 
      3L), .Label = c("I eat cookies.", "I like to run and play.", 
      "I like to run, play, swim, and eat.", "I like to sleep.", 
      "I went to swim in the pool."), class = "factor")), class = 
      "data.frame", row.names = c(NA, -5L))
      words <- c("Run","Play","Eat","Swim")
      

      【讨论】:

      • 有没有办法创建一个循环来循环遍历数据框和列表?上面的数据框和单词列表就是一个例子。我正在使用的数据框包含 600 条评论,列表包含大约 40 个单词。
      • @JaylonAaron 没关系。这应该仍然有效。您的单词列表是如何存储的?
      • 我让它工作了。我必须先取消列表中的单词,然后再运行它。感谢您的帮助!
      • @JaylonAaron 如果您觉得它对您有用,请点击投票按钮旁边的复选标记,随时accept the answer。 :-) 每个帖子只能接受一个答案。
      【解决方案3】:

      Base R 解决方案(注意此解决方案有意不区分大小写):

      # Create a vector of patterns to search for: 
      
      patterns <- c("Run", "Play", "Eat", "Swim")
      
      # Split on the review number, apply a term counting function (for each review number): 
      
      df$term_count <- sapply(split(df, df$Review), 
      
                              function(x){length(grep(paste0(tolower(patterns), collapse = "|"),
      
                                     tolower(unlist(strsplit(x$Text, "\\s+")))))})
      

      数据:

      df <- data.frame(Review = 1:5, Text = as.character(c("I like to run and play",
                                                           "I eat cookies",
                                                           "I went to swim in the pool.",
                                                           "I like to sleep.", 
                                                           "I like to run, play, swim, and eat.")), 
                       stringsAsFactors = FALSE)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-12-29
        • 1970-01-01
        • 2022-08-13
        • 2021-10-15
        • 1970-01-01
        • 2022-06-21
        • 1970-01-01
        相关资源
        最近更新 更多