Load the required packages

library(twitteR); library(tidyverse)
library(lubridate);library(tidytext)
ge <- read.csv("GE2020_07_04.csv")

Break #GE2020 data into media and public

media <- c("sgelection", "STcom", "asiaonecom", "BusinessTimes",
           "ChannelNewsAsia", "sgelectionswoke", "PAPSingapore",
           "MothershipSG", "YahooSG", "SPP_SG")
'%ni%' <- Negate('%in%')
ge_media <- ge %>% filter(isRetweet==FALSE) %>%
  filter(screenName %in% media)
ge_public <- ge %>% filter(isRetweet==FALSE) %>%
  filter(screenName %ni% media)

Visualize count of tweets by screenName (top 10)

top10users <- ge_public %>% count(screenName, sort = T) %>% top_n(10) %>%
  ungroup()
## Selecting by n
ggplot(top10users, aes(fct_inorder(screenName), n)) +
  geom_bar(stat = "identity") +
  ggtitle("#GE2020 Top 10 tweeters as at 4 July") +
  theme(axis.text.x = element_text(angle = 45))

Convert ge_pubic tweets into a tibble

tweets_text <- ge_public$text
length(tweets_text)
## [1] 1583
tweets_tbl <- tibble(line =1:1583, text = tweets_text)
twitter_stop_words <- tibble(word = c("https","t.co","rt","amp",
                                      "rstats","gt"), lexicon = "twitter")
ge_stop_words <- tibble(word = c("ge2020", "party", "singapore", "election",
                                 "#singapore", "july", "10", "day",
                                 "sgelection", "singaporevotes", "sgvotes",
                                 "sgelections", "sgelection2020"),
                        lexixon = "GE")
data("stop_words")

Public opinion of GE2020 (Trigram)

tri_po <- tweets_tbl %>%
  unnest_tokens(trigram, text, token = "ngrams", n = 3)
trigrams_separated <- tri_po %>%
  separate(trigram, c('word1',  'word2', 'word3'), sep = " ")

trigrams_filtered <- trigrams_separated %>%
  filter(!word1 %in% stop_words$word) %>%
  filter(!word2 %in% stop_words$word) %>%
  filter(!word3 %in% stop_words$word) %>%
  filter(!word1 %in% twitter_stop_words$word) %>%
  filter(!word2 %in% twitter_stop_words$word) %>%
  filter(!word3 %in% twitter_stop_words$word) %>%
  filter(!word1 %in% ge_stop_words$word) %>%
  filter(!word2 %in% ge_stop_words$word) %>%
  filter(!word3 %in% ge_stop_words$word)
trigrams_filtered %>% count(word1, word2, word3, sort = T)
## # A tibble: 1,608 x 4
##    word1 word2   word3       n
##    <chr> <chr>   <chr>   <int>
##  1 east  coast   plan       14
##  2 heng  swee    keat       14
##  3 tan   cheng   bock       12
##  4 ch    5       media      11
##  5 mao   god     omg        11
##  6 5     media   corp       10
##  7 east  coast   grc        10
##  8 god   omg     omaogod     8
##  9 omg   omaogod cdc         8
## 10 ong   ye      kung        8
## # ... with 1,598 more rows

Public Opinion of East Coast GRC

ec <- tweets_tbl %>% 
  unnest_tokens(sentence, text, token = "sentences") %>%
  filter(str_detect(sentence, "east coast"))
c(ec[,2])
## $sentence
##  [1] "constituency broadcast day 2: nothing really spectacular, but nicole and her east coast #wp team really looks stron… https://t.co/wey9jfu0ez"
##  [2] "during #ge2020 : as part of the constituency plan, a new mrt station will be built in *cough*east coast*cough*  aft… https://t.co/xey2hu4wni"
##  [3] "ok #wp’s east coast team is actually quite strong."                                                                                          
##  [4] "<u+0001f6a8> we finally have the east coast plan <u+0001f6a8> #ge2020 https://t.co/nuyjlxzk7r"                                               
##  [5] "the mayor of the east coast of the south east."                                                                                              
##  [6] "pulau tekong is in east coast grc, imagine if recruits were allowed to vote as part of it. #ge2020"                                          
##  [7] "if he was truly a genius, he would say something about \"the east coast plan\"."                                                             
##  [8] "sir,  ungala ippo thaan east coast'ku transfer pannanga....."                                                                                
##  [9] "constituency broadcast: ooh, hsk and his east coast plan lai liao!"                                                                          
## [10] "east coast plan east coast plan east coast plan #ge2020 #sgvotes"                                                                            
## [11] "i’ve been reading the fabled east coast plan and the question that keeps coming up is, where does constituency end… https://t.co/xltdkfdpwj" 
## [12] "#singapore #ge2020   east coast grc will be a tough arena if one party does not have a plan ~ https://t.co/cc9y9xvswh"                       
## [13] "as a voter i know that climate change really impacts people that live in east coast grc."                                                    
## [14] "the east coast disco plan!"                                                                                                                  
## [15] "conspiracy theory: the east coast plan is to have lhy as pm <u+0001f92f><u+0001f92f><u+0001f92f> #ge2020"                                    
## [16] "constituency broadcast: tomororw got east coast grc!"                                                                                        
## [17] "a man, a plan, a together, we care @ east coast, a canal #sgmemes community submission @momo8401 #ge2020 #gememes… https://t.co/mmcsc0otpw"  
## [18] "the good thing about campaigning in bedok/east coast - there are so many hawker centres."                                                    
## [19] "#ge2020 dpm heng swee keat on a walkabout in bedok north, joined by outgoing east coast mp lee yi shyan."                                    
## [20] "hsk east coast plan is to be blamed for this #eastcoastplan #ge2020 https://t.co/gizwv8g5ku"                                                 
## [21] "this must be their east coast plan."                                                                                                         
## [22] "mr east coast plan is going to be representing the pap for the party political broacast tonight."                                            
## [23] "people of east coast, aljunied, hougang and so on..."                                                                                        
## [24] "esm goh and dr tan interact with exercising residents at east coast park #ge2020 https://t.co/mp1abx3vvg"                                    
## [25] "#singapore #ge2020  tampines grc will certainly  miss dpm heng swee keat  @papsingapore who has moved to east coast… https://t.co/cnx1xyn3sl"
## [26] "i think we need no blank cheque tote bags to go with east coast plan t-shirts #ge2020 https://t.co/g8wlmiihkl"                               
## [27] "pap inserted heng swee keat into the east coast grc team bc they were thinking that singaporeans will not vote away… https://t.co/aiy4latixu"
## [28] "which means they shouldn’t have any problem having wp run east coast grc yeah?"                                                              
## [29] "waddup east coast #ge2020 #visualinconsideration https://t.co/00jmgcgxqj"                                                                    
## [30] "east coast, gold coast?"                                                                                                                     
## [31] "now that the debate is over, i am pivoting my full attention to the east coast rally."                                                       
## [32] "missing out on the televised #ge2020 debate, because tuning into east coast grc’s @papsingapore live fb broadcast.… https://t.co/dwg5na5uuy" 
## [33] "the story behind the east coast plan!"                                                                                                       
## [34] "yeah , the whole of singapore wants to migrate to east coast in anticipation of “the plan”. ."                                               
## [35] "this morning, the five @wpsg candidates standing in east coast grc were out and about at bedok 85 market and food c… https://t.co/yl9wy7gbpw"
## [36] "it’s called the east coast flan.… https://t.co/b6ai7dnvha"                                                                                   
## [37] "\"i want to introduce our plans for east coast later."                                                                                       
## [38] "\"er... sir they are 'east coast @ singapore… https://t.co/xpok8lznln"                                                                       
## [39] "seeing too many people asking east coast grc residents to vote for wp so that heng swee keat will be out and we wil… https://t.co/ej5dlia4oq"
## [40] "still trying to find out what is the east coast plan!"                                                                                       
## [41] "there is “obviously an element of strategic surprise” in heng swee keat’s 11th-hour move to east coast grc, says wp… https://t.co/hxim5ceqss"
## [42] "east coast voter here waiting for the inevitable guilt tripping from the incumbent party."                                                   
## [43] "how i wish i was voting in aljunied or east coast.. so i can feel the future of singapore depends on my vote<u+0001f602> #ge2020"            
## [44] "#ge2020: workers’ party chief pritam singh joined the five-man east coast team, including nicole seah, in a morning… https://t.co/hkglmttrv2"
## [45] "some idiots are claiming that if they don't vote for dpm heng at east coast grc, tharman will be pm."