【问题标题】:Removing stop words with tidytext使用 tidytext 删除停用词
【发布时间】:2017-04-16 20:36:42
【问题描述】:

使用 tidytext,我有这个代码:

data(stop_words)
tidy_documents <- tidy_documents %>%
      anti_join(stop_words)

我希望它使用包中内置的停用词将名为 tidy_documents 的数据帧写入同名数据帧,但如果它们在 stop_words 中,则将其删除。

我收到此错误:

错误:没有公共变量。请指定by 参数。 追溯:

1. tidy_documents %>% anti_join(stop_words)
2. withVisible(eval(quote(`_fseq`(`_lhs`)), env, env))
3. eval(quote(`_fseq`(`_lhs`)), env, env)
4. eval(expr, envir, enclos)
5. `_fseq`(`_lhs`)
6. freduce(value, `_function_list`)
7. withVisible(function_list[[k]](value))
8. function_list[[k]](value)
9. anti_join(., stop_words)
10. anti_join.tbl_df(., stop_words)
11. common_by(by, x, y)
12. stop("No common variables. Please specify `by` param.", call. = FALSE)

【问题讨论】:

  • 显然tidy_documentsstop_words 不共享任何变量名,因此您需要使用by 参数来匹配这两个数据集。
  • stop_words 的列被称为word,因此要么命名你的列,要么使用anti_joinby 参数。
  • tidy_documents 中的列名是什么?如果您分享,我们可以具体告诉您如何设置加入。
  • @JuliaSilge tidy_documents 中的列是 `author;日期;词'。
  • @textnet 嗯,那看起来很奇怪。如果您的主数据集中有 word 列,我希望 anti_join() 知道将其与 stop_words 数据集中的 word 列匹配。可以试试make a reproducible example 有数据吗?

标签: r dplyr tidyverse tidytext


【解决方案1】:

您可以使用更简单的filter() 来避免像这样使用令人困惑的anti_join() 函数:

tidy_documents <- tidy_documents %>%
  filter(!word %in% stop_words$word)

【讨论】:

    【解决方案2】:

    tidy_documentstop_words 在名为 word 的列下都有一个单词列表;但是,列是倒置的:在stop_words 中,它是第一列,而在您的数据集中,它是第二列。这就是为什么该命令无法“匹配”两列并比较单词的原因。试试这个:

    tidy_document <- tidy_document %>% 
          anti_join(stop_words, by = c("word" = "word"))
    

    by 命令强制脚本比较称为word 的列,而不管它们的位置。

    【讨论】:

      猜你喜欢
      • 2019-08-11
      • 2021-09-26
      • 2019-09-12
      • 2013-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-02
      • 2016-05-21
      相关资源
      最近更新 更多