使用 tidyverse 包在 R 中进行情感分析 - 未找到对象 'sentiment'

Sentiment Analysis in R with tidyverse package - object 'sentiment' not found

我正在尝试重现这个情绪分析示例:https://www.kaggle.com/rtatman/tutorial-sentiment-analysis-in-r

我在“../input”文件夹中有一个 "file.txt",其中包含我要分析的文本。

library(tidyverse)
library(tidytext)
library(glue)
library(stringr)
library(dplyr)
require(plyr)

# get a list of the files in the input directory
files <- list.files("../input")
fileName <- glue("../input/", files[1], sep = "")
fileName <- trimws(fileName)
fileText <- glue(read_file(fileName))
fileText <- gsub("\$", "", fileText)
tokens <- data_frame(text = fileText) %>% unnest_tokens(word, text)

但在这一行之后

#get the sentiment from the first text: 
tokens %>%
  inner_join(get_sentiments("bing")) %>% # pull out only sentiment words
  count(sentiment) %>% # count the # of positive & negative words
  spread(sentiment, n, fill = 0) %>% # made data wide rather than narrow
  mutate(sentiment = positive - negative) # # of positive words - # of negative owrds

我收到一条错误消息

Error in count(., sentiment) : object 'sentiment' not found

昨天同样的代码工作正常,今天我得到这个错误。看来问题是由 plyr 包引起的。在 dplyr 之前加载 plyr 时似乎工作正常,但现在即使按该顺序加载它们也会出错。

问题是由 plyr 包与 dplyr 一起加载引起的。我用 this approach 来使用 plyr 而没有加载它,现在代码运行没有任何错误。

我运行同样的错误,即使不加载plyr包,你也可以在调用"count"函数时使用显式包修复它:

dplyr::count(sentiment)

总的来说应该是这样的:

#get the sentiment from the first text: 
tokens %>%
  inner_join(get_sentiments("bing")) %>% # pull out only sentiment words
  dplyr::count(sentiment) %>% # count the # of positive & negative words
  spread(sentiment, n, fill = 0) %>% # made data wide rather than narrow
  mutate(sentiment = positive - negative) # # of positive words - # of negative owrds