This is an R Markdown Notebook. When you execute code within the notebook, the results appear beneath the code.
Try executing this chunk by clicking the Run button within the chunk or by placing your cursor inside it and pressing Cmd+Shift+Enter.
Install necessary packages
# install.packages('tm')
# install.packages('RColorBrewer')
# install.packages('wordcloud')
library('tm')
## Loading required package: NLP
library('RColorBrewer')
library('wordcloud')
Process data
# Import data from a csv file to data frame
Trump <- read.csv("Trump.csv", comment.char="#")
maleTw <- subset(Trump, USER_GENDER == "male")
femaleTw <- subset(Trump, USER_GENDER == "female")
# Save and read data to/from a R data object
saveRDS(maleTw, "maleTw.RDS")
maleTw <- readRDS("maleTw.RDS")
Mtweets <- maleTw$MESSAGE_BODY
Ftweets <- femaleTw$MESSAGE_BODY
# Function to clean tweets
clean.text = function(x)
{
# tolower
x = tolower(x)
# remove rt
x = gsub("rt", "", x)
# remove at
x = gsub("@\\w+", "", x)
# remove punctuation
x = gsub("[[:punct:]]", "", x)
# remove numbers
x = gsub("[[:digit:]]", "", x)
# remove links http
x = gsub("http\\w+", "", x)
# remove tabs
x = gsub("[ |\t]{2,}", "", x)
# remove blank spaces at the beginning
x = gsub("^ ", "", x)
# remove blank spaces at the end
x = gsub(" $", "", x)
return(x)
}
# clean tweets
Mtweets = clean.text(Mtweets)
Ftweets = clean.text(Ftweets)
city<- subset(Trump,USER_CITY=="WASHINGTON")
cityn<- subset(Trump,USER_CITY=="NEW YORK CITY")
cityn <- cityn$MESSAGE_BODY
city <- city$MESSAGE_BODY
cityn<- clean.text(cityn)
city<- clean.text(city)
Create word cloud of tweets of WASHINGTON
## amp point republican answer can
## 3 3 3 2 2
## new theyre michigan win beat
## 2 2 2 2 2
## amiright tough nhprimary trumps good
## 2 2 2 2 2
## donaldtrump planelection tax campaign thousands
## 2 2 2 2 2
Create word cloud of tweets of New York City
## donaldtrump forelection retweet voting wondering
## 2 2 2 2 2
## trumpflprimary will bratty child gopclowncar
## 2 2 1 1 1
## never noelection told interview next
## 1 1 1 1 1
## president standing states united witham
## 1 1 1 1 1