【问题标题】:Error when using tidytext to calculate word frequencies in R使用 tidytext 计算 R 中的词频时出错
【发布时间】:2018-07-13 09:17:08
【问题描述】:

我一直在尝试使用 tidytext 包计算词频。

v <- "Everybody dance now! Give me the music Everybody dance now! Give me the music Everybody dance now! Everybody dance now! Yeah! Yeah! Yeah!"
v <- as.character(v)
v %>% count(words)

但我一直收到此错误: UseMethod("as.quoted") 中的错误: 没有适用于“函数”类对象的“as.quoted”方法

请帮忙!谢谢!

【问题讨论】:

  • 我认为 tidytext 本身没有 count 方法。 tidytext 可以帮助您将文档或字符串转换为数据框,其中包含您可以计算的单词。请参阅下面的答案。

标签: r string text-mining tidytext


【解决方案1】:

tidytext 是允许您将字符串(在数据帧中)转换为单词和其他内容的包。您可以将字符串转换为数据框,然后使用tidytext 方法unnest_tokens 将其转换为单词,然后使用dplyr 转换为group_by 单词,然后使用count 它们:

tibble(v) %>% tidytext::unnest_tokens(word, v) %>% group_by(word) %>% count()
# A tibble: 8 x 2
# Groups:   word [8]
  word          n
  <chr>     <int>
1 dance         4
2 everybody     4
3 give          2
4 me            2
5 music         2
6 now           4
7 the           2
8 yeah          3

【讨论】:

    【解决方案2】:

    我正在处理一个类似的案例,调用 dplyr 与 count() 函数一起工作:

    tokens %>%
    # call dplyr   
    dplyr::count(word)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-19
      • 1970-01-01
      • 2019-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-16
      相关资源
      最近更新 更多