带有管道的 tidytext 示例过滤器错误

tidytext example filter error with pipes

尝试重现 http://tidytextmining.com/twitter.html 中的示例时出现问题。

基本上我想改编这部分代码

library(tidytext)
library(stringr)

reg <- "([^A-Za-z_\d#@']|'(?![A-Za-z_\d#@]))"

tidy_tweets <- tweets %>% 
    mutate(text = str_replace_all(text, "https://t.co/[A-Za-z\d]+|http://[A-Za-z\d]+|&amp;|&lt;|&gt;|RT", "")) %>%
    unnest_tokens(word, text, token = "regex", pattern = reg) %>%
    filter(!word %in% stop_words$word,
        str_detect(word, "[a-z]"))

为了保留 stop_Word 包含的推文数据框。

所以我尝试了这个:

tidy_tweets <- tweets %>% 
    mutate(text = str_replace_all(text, "https://t.co/[A-Za-z\d]+|http://[A-Za-z\d]+|&amp;|&lt;|&gt;|RT", "")) %>%
    unnest_tokens(word, text, token = "regex", pattern = reg) 

tidy_tweets_sw <- filter(!word %in% stop_words$word, str_detect(tidy_tweets, "[a-z]"))

但这没有用,因为我收到以下错误消息:

Error in match(x, table, nomatch = 0L) :  
'match' requires vector arguments

我试图传递两个输入的矢量版本以进行匹配,但无济于事。 有人有更好的主意吗?

您需要将 filter 语句中的数据作为第一个参数。

tidy_tweets <- tweets %>% 
  mutate(text = str_replace_all(text, "https://t.co/[A-Za-z\d]+|http://[A-Za-z\d]+|&amp;|&lt;|&gt;|RT", "")) %>%
  unnest_tokens(word, text, token = "regex", pattern = reg) 

tidy_tweets_sw <- filter(tidy_tweets, !(word %in% stop_words$word), str_detect(tidy_tweets, "[a-z]"))

不确定,但我认为你的问题出在这里:

tidy_tweets_sw <- filter(!word %in% stop_words$word, str_detect(tidy_tweets, "[a-z]"))

filter 完全不知道您要过滤什么,这应该有效:

tidy_tweets_sw <- tidy_tweets %>% filter(!word %in% stop_words$word, str_detect(tidy_tweets, "[a-z]"))