Install necessary packages

# install.packages('tm')
# install.packages('RColorBrewer')
# install.packages('wordcloud')
library('tm')
## Warning: package 'tm' was built under R version 3.4.4
## Loading required package: NLP
library('RColorBrewer')
library('wordcloud')
## Warning: package 'wordcloud' was built under R version 3.4.4

Process data

Zynga <- readRDS("D:\\New folder (7)\\Zynga.RDS")


Ztweets <- Zynga$text


clean.text = function(x)
{
  
  x = tolower(x)
 
  x = gsub("rt", "", x)
  
  x = gsub("@\\w+", "", x)
  
  x = gsub("[[:punct:]]", "", x)
 
  x = gsub("[[:digit:]]", "", x)
  
  x = gsub("http\\w+", "", x)
  
  x = gsub("[ |\t]{2,}", "", x)
 
  x = gsub("^ ", "", x)
 
  x = gsub(" $", "", x)

  return(x)
  
}
Encoding(Ztweets)  <- "UTF-8"
library('stringr')
## Warning: package 'stringr' was built under R version 3.4.3
Ztweets=str_replace_all(Ztweets,"[^[:graph:]]", " ") 


Zyngatweets = clean.text(Ztweets)

Wordcloud

corpus = Corpus(VectorSource(Zyngatweets))

tdm = TermDocumentMatrix(
  corpus,
  control = list(
    wordLengths=c(3,20),
    removePunctuation = TRUE,
    stopwords = c("the", "a", stopwords("english")),
    removeNumbers = TRUE, tolower = TRUE) )


tdm = as.matrix(tdm)


word_freqs = sort(rowSums(tdm), decreasing=TRUE) 


word_freqs = word_freqs[-(1:9)]  


dm = data.frame(word=names(word_freqs), freq=word_freqs)


wordcloud(head(dm$word, 50), head(dm$freq, 50), random.order=FALSE, colors=brewer.pal(8, "Dark2"))

head(word_freqs, 10)
##   looking       can    prized      play      â€ï     adult     petra 
##       258       232       219       207       200       187       187 
##      game    jeneva     found 
##       179       175       167

histogram1 sentiment

pos.words = scan('C:\\Users\\Xiayang Xiao\\Downloads\\positive-words.txt', what='character', comment.char=';')
neg.words = scan('C:\\Users\\Xiayang Xiao\\Downloads\\negative-words.txt', what='character', comment.char=';')

neg.words = c(neg.words, 'wtf', 'fail')


require(plyr)
## Loading required package: plyr
## Warning: package 'plyr' was built under R version 3.4.3
require(stringr)
require(stringi)
## Loading required package: stringi
score.sentiment = function(sentences, pos.words, neg.words, .progress='none')
{
  
 
  scores = laply(sentences, function(sentence, pos.words, neg.words) {
    
    
    sentence = gsub('[[:punct:]]', '', sentence)
    sentence = gsub('[[:cntrl:]]', '', sentence)
    sentence = gsub('\\d+', '', sentence)
    sentence = tolower(sentence)
    
    word.list = str_split(sentence, '\\s+')
   
    words = unlist(word.list)
    
   
    pos.matches = match(words, pos.words)
    neg.matches = match(words, neg.words)
    
    
    pos.matches = !is.na(pos.matches)
    neg.matches = !is.na(neg.matches)
    
   
    score = sum(pos.matches) - sum(neg.matches)
    
    return(score)
  }, pos.words, neg.words, .progress=.progress )
  
  scores.df = data.frame(score=scores, text=sentences)
  return(scores.df)
}

sentiment.scores= score.sentiment(Zyngatweets, pos.words, neg.words, .progress='none')

score <- sentiment.scores$score

library(plotly)
## Warning: package 'plotly' was built under R version 3.4.3
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 3.4.3
## 
## Attaching package: 'ggplot2'
## The following object is masked from 'package:NLP':
## 
##     annotate
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following objects are masked from 'package:plyr':
## 
##     arrange, mutate, rename, summarise
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
p <- plot_ly(x = ~score, type = "histogram")
p
## Warning: package 'bindrcpp' was built under R version 3.4.3

histogram2 weekday

Zynga$days <- weekdays(as.POSIXlt(Zynga$created))
p <- plot_ly(x = ~Zynga$days, type = "histogram")
p