简体   繁体   中英

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

I am trying to reproduce this exmple of sentiment analysis:https://www.kaggle.com/rtatman/tutorial-sentiment-analysis-in-r

I have a "file.txt" with the text I want to analyze in "../input" folder.

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)

but after this line

#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

I get an error message

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

Yesterday the same code worked fine, and today I get this error. It appears the problem is cause by plyr package. It seemed to work fine when plyr was loaded before dplyr , but now gives an error even if they are loaded in that order.

The problem was caused by plyr package being loaded together with dplyr . I used this approach to use plyr without loading it and the code runs without any errors now.

I ran into the same error, even without loading the plyr package, you can fix it by using the explicit package when calling the "count" function:

dplyr::count(sentiment)

Alltogether it should look like this:

#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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM