THANKSGIVING TWEETS

## [1] "Using direct authentication"

Top 10 Thanksgiving tweets

num_tweets = 10 
turkey_tweets = searchTwitter('#thanksgiving', n = num_tweets)
turkey_tweets
## [[1]]
## [1] "MarySager0622: Post Thanksgiving exhaustion #Thanksgiving  #14catsareenough #nap https://t.co/dPcp6P6SY3"
## 
## [[2]]
## [1] "GaudetMatthew: Spicy miso turkey ramen for Sunday dinner? Sure!\n#leftovers #thanksgiving #sunday #sundaydinner<U+0085> https://t.co/oR0bbnVmR1"
## 
## [[3]]
## [1] "jpuopolo: Did you have a good weekend and #Thanksgiving ?"
## 
## [[4]]
## [1] "wmjackson: #Thanksgiving was #dope so much fun!!\nFellowship and family and the kids had a great time that was awesome and mom has a #Christmas tree!!!"
## 
## [[5]]
## [1] "AngeDeusRex: RT @AngeDeusRex: -#DestinationSundays at @TunnelChicago, live tonight! Dance off some of that #Thanksgiving dinner with some #salsa, #meren<U+0085>"
## 
## [[6]]
## [1] "RichelleLWright: #thanksgiving #withquebecfamandfriends #everheardof #bobtheweasel<U+0085> https://t.co/QZHx9XVo4z"
## 
## [[7]]
## [1] "cadamo24: My family<U+0092>s #heartfelt #thanksgiving #tradition is buying an entire other turkey when they go on sale and remaking<U+0085> https://t.co/dc0Xj4yufs"
## 
## [[8]]
## [1] "Mikey_TheAgora: #Thanksgiving may be over, but it's never out of style to be #thankful I'm thankful for family, friends, and the op<U+0085> https://t.co/KrO5Y1HzjZ"
## 
## [[9]]
## [1] "Freebies4Mom: Gobble Gobble!\n\nTurkey Cookies for #Thanksgiving - they're easy to make!\n\nhttps://t.co/bbI5iUKeHO #ad https://t.co/pyxPZ1CQq4"
## 
## [[10]]
## [1] "RangerLady: #thanksgiving #family #2017 https://t.co/VCdO7yyOTG"

Turkey tweets converted to a dataframe

turkey_df = twListToDF(turkey_tweets)
head(turkey_tweets)
## [[1]]
## [1] "MarySager0622: Post Thanksgiving exhaustion #Thanksgiving  #14catsareenough #nap https://t.co/dPcp6P6SY3"
## 
## [[2]]
## [1] "GaudetMatthew: Spicy miso turkey ramen for Sunday dinner? Sure!\n#leftovers #thanksgiving #sunday #sundaydinner<U+0085> https://t.co/oR0bbnVmR1"
## 
## [[3]]
## [1] "jpuopolo: Did you have a good weekend and #Thanksgiving ?"
## 
## [[4]]
## [1] "wmjackson: #Thanksgiving was #dope so much fun!!\nFellowship and family and the kids had a great time that was awesome and mom has a #Christmas tree!!!"
## 
## [[5]]
## [1] "AngeDeusRex: RT @AngeDeusRex: -#DestinationSundays at @TunnelChicago, live tonight! Dance off some of that #Thanksgiving dinner with some #salsa, #meren<U+0085>"
## 
## [[6]]
## [1] "RichelleLWright: #thanksgiving #withquebecfamandfriends #everheardof #bobtheweasel<U+0085> https://t.co/QZHx9XVo4z"

Top 10 retweeted Thanksgiving tweets

turkey_df %>% group_by(text, retweetCount) %>% summarise(n = n()) %>%
  arrange(desc(retweetCount)) %>% top_n(10)
## Selecting by n
## # A tibble: 10 x 3
## # Groups:   text [10]
##                                                                           text
##                                                                          <chr>
##  1 RT @AngeDeusRex: -#DestinationSundays at @TunnelChicago, live tonight! Danc
##  2                         #thanksgiving #family #2017 https://t.co/VCdO7yyOTG
##  3 #thanksgiving #withquebecfamandfriends #everheardof #bobtheweasel<U+0085> https://
##  4 #Thanksgiving may be over, but it's never out of style to be #thankful I'm 
##  5 "#Thanksgiving was #dope so much fun!!\nFellowship and family and the kids 
##  6                             Did you have a good weekend and #Thanksgiving ?
##  7 "Gobble Gobble!\n\nTurkey Cookies for #Thanksgiving - they're easy to make!
##  8 My family<U+0092>s #heartfelt #thanksgiving #tradition is buying an entire other t
##  9 Post Thanksgiving exhaustion #Thanksgiving  #14catsareenough #nap https://t
## 10 "Spicy miso turkey ramen for Sunday dinner? Sure!\n#leftovers #thanksgiving
## # ... with 2 more variables: retweetCount <dbl>, n <int>

Top 5 words

reg <- "([^A-Za-z\\d#@']|'(?![A-Za-z\\d#@]))"
turkey_words <- turkey_df %>%
  filter(!str_detect(text, '^"')) %>%
  mutate(text = str_replace_all(text, "https://t.co/[A-Za-z\\d]+|&amp;", "")) %>%
  unnest_tokens(word, text, token = "regex", pattern = reg) %>%
  filter(!word %in% stop_words$word,
         str_detect(word, "[a-z]"))

turkey_words %>% group_by(word) %>% summarize(n = n()) %>% arrange(desc(n)) %>% top_n(5)
## Selecting by n
## # A tibble: 5 x 2
##            word     n
##           <chr> <int>
## 1 #thanksgiving    10
## 2        family     3
## 3        turkey     3
## 4        dinner     2
## 5        gobble     2

Sentiments associated with turkey tweets

nrc <- sentiments %>%
  filter(lexicon == "nrc") %>%
  select(word, sentiment)

turkey_words_sentiments <- turkey_words %>% inner_join(nrc, by = "word")

turkey_words_sentiments %>% group_by(sentiment) %>% summarize(n = n()) %>% arrange(desc(n))
## # A tibble: 9 x 2
##      sentiment     n
##          <chr> <int>
## 1     positive     8
## 2          joy     5
## 3 anticipation     4
## 4        trust     2
## 5        anger     1
## 6      disgust     1
## 7     negative     1
## 8      sadness     1
## 9     surprise     1

Regexing one of the tweets

phrase = "A few days to #Thanksgiving . Time to start counting our #blessings..."
reg = "([^A-Za-z\\d#@']|'(?![A-Za-z\\d#@]))"
df <- data.frame(string = 1, text = phrase)
df %>% unnest_tokens(word, text, token = "regex", pattern = reg)
##    string          word
## 1       1             a
## 2       1           few
## 3       1          days
## 4       1            to
## 5       1 #thanksgiving
## 6       1          time
## 7       1            to
## 8       1         start
## 9       1      counting
## 10      1           our
## 11      1    #blessings

Removing “stop words”

df %>% unnest_tokens(word, text, token = "regex", pattern = reg) %>%
  filter(!word %in% stop_words$word)
##   string          word
## 1      1          days
## 2      1 #thanksgiving
## 3      1          time
## 4      1         start
## 5      1      counting
## 6      1    #blessings

```