1

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

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

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

我收到一条错误消息

计数错误(。,情绪):找不到对象“情绪”

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

4

2 回答 2

0

该问题是由于plyrdplyr. 我使用这种方法plyr不加载它的情况下使用它,并且代码现在运行没有任何错误。

于 2018-02-21T00:34:31.127 回答
0

我遇到了同样的错误,即使没有加载 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
于 2021-11-15T19:32:06.570 回答