Search code examples
rsentiment-analysistidytext

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.


Solution

  • 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.