First at all, we install two new packages “rtweet” and “httpuv” and load them into the RStudio.
if (!require("rtweet")) install.packages("rtweet", repos="https://cran.cnr.berkeley.edu/", dependencies = TRUE)
if (!require("httpuv")) install.packages("httpuv", repos="https://cran.cnr.berkeley.edu/", dependencies = TRUE)
require("rtweet") # load the required library
require("httpuv")
require("plotly")
Then, please insert your appname, consumerKey, and consumerSecret into the three different “character” variables as shown in the following code.
appname <- "YOUR APP NAME"
consumerKey <- "YOUR CONSUMER KEY"
consumerSecret <- "YOUR CONSUMER SECRET"
Setup your Twitter token …..
twitter_token <- create_token(app = appname, consumer_key = consumerKey, consumer_secret = consumerSecret, set_renv =TRUE)
Once the Twitter API token is ready, we use the command “search_tweets” to search Twitter for posts containing the keyword “#brexit”. The returned object (100 tweets, no retweets) is in data frame and [5,] and $COLUMN_NAME are used respectively to display the 5th row and column of the data frame.
brexit <- search_tweets("#brexit", n = 500, include_rts = FALSE)
class(brexit) # Show its data class
## [1] "tbl_df" "tbl" "data.frame"
colnames(brexit) # Show all its columns
## [1] "user_id" "status_id"
## [3] "created_at" "screen_name"
## [5] "text" "source"
## [7] "display_text_width" "reply_to_status_id"
## [9] "reply_to_user_id" "reply_to_screen_name"
## [11] "is_quote" "is_retweet"
## [13] "favorite_count" "retweet_count"
## [15] "hashtags" "symbols"
## [17] "urls_url" "urls_t.co"
## [19] "urls_expanded_url" "media_url"
## [21] "media_t.co" "media_expanded_url"
## [23] "media_type" "ext_media_url"
## [25] "ext_media_t.co" "ext_media_expanded_url"
## [27] "ext_media_type" "mentions_user_id"
## [29] "mentions_screen_name" "lang"
## [31] "quoted_status_id" "quoted_text"
## [33] "quoted_created_at" "quoted_source"
## [35] "quoted_favorite_count" "quoted_retweet_count"
## [37] "quoted_user_id" "quoted_screen_name"
## [39] "quoted_name" "quoted_followers_count"
## [41] "quoted_friends_count" "quoted_statuses_count"
## [43] "quoted_location" "quoted_description"
## [45] "quoted_verified" "retweet_status_id"
## [47] "retweet_text" "retweet_created_at"
## [49] "retweet_source" "retweet_favorite_count"
## [51] "retweet_retweet_count" "retweet_user_id"
## [53] "retweet_screen_name" "retweet_name"
## [55] "retweet_followers_count" "retweet_friends_count"
## [57] "retweet_statuses_count" "retweet_location"
## [59] "retweet_description" "retweet_verified"
## [61] "place_url" "place_name"
## [63] "place_full_name" "place_type"
## [65] "country" "country_code"
## [67] "geo_coords" "coords_coords"
## [69] "bbox_coords" "status_url"
## [71] "name" "location"
## [73] "description" "url"
## [75] "protected" "followers_count"
## [77] "friends_count" "listed_count"
## [79] "statuses_count" "favourites_count"
## [81] "account_created_at" "verified"
## [83] "profile_url" "profile_expanded_url"
## [85] "account_lang" "profile_banner_url"
## [87] "profile_background_url" "profile_image_url"
brexit[5,]$text ### The text of the status
## [1] "This deeply dangerous move towards an EU Army at best will duplicate @NATO and at worst will usurp this tested defence posture which has not lost a square inch of territory since its inception 70 years ago. #Brexit https://t.co/FkzwtAtvaS"
brexit[5,]$screen_name ### Screen name of the user who posted this status
## [1] "DKShrewsbury"
brexit[5,"screen_name"]
## # A tibble: 1 x 1
## screen_name
## <chr>
## 1 DKShrewsbury
brexit[5:10,"screen_name"]
## # A tibble: 6 x 1
## screen_name
## <chr>
## 1 DKShrewsbury
## 2 Esther_767
## 3 Deebhoy
## 4 prosser386
## 5 Mill_Handy
## 6 SKSMediaIndia
brexit[5,]$created_at ### When this status was created
## [1] "2019-01-25 06:30:48 UTC"
brexit[5,]$retweet_count ### The number of times this status has been retweeted
## [1] 0
mean(brexit$followers_count) # mean followers count of the post's authors
## [1] 19591.99
median(brexit$followers_count) # median followers count of the post's authors
## [1] 752
plot_ly(x = ~brexit$followers_count, type = "histogram") # Show a histogram
## Exercise: Try a large number by setting
## Lookup the rate limit table: https://developer.twitter.com/en/docs/basics/rate-limits.html
##rt <- search_tweets("#brexit", n = 18000)
##rt <- search_tweets("#brexit", n = 25000, retryonratelimit = TRUE) # beyond 15 mins rate limit
Next, we analyze the profile of handle “StandUp4Brexit”, which is a “Conservative grassroots campaign to ensure #Brexit as promised in the Conservative manifesto is delivered.” Again, the returned values are in a data frame and we can use $ to show the columns.
standup4brexit <- lookup_users("StandUp4Brexit")
standup4brexit$name # Name of the user
## [1] "#StandUp4Brexit"
standup4brexit$followers_count # Followers count
## [1] 18295
standup4brexit$description # User's description
## [1] "Conservative grassroots campaign to ensure #Brexit as promised in the Conservative manifesto is delivered #StandUp4Brexit | Donate at: https://t.co/7OeLCsdU0v"
standup4brexit$location # Location
## [1] "United Kingdom"
It is interesting to check who are following this handle. We deploy the command “get_followers” to collect the list of its followers and use [,] and $ to display the contents of the data frame. Next, we use “lookup_users” to obtain the individual follower’s profile.
standup4brexit_folls <- get_followers("StandUp4Brexit")
nrow(standup4brexit_folls) # Look at the size of the data
## [1] 5000
head(standup4brexit_folls) # First 6 followers
## # A tibble: 6 x 1
## user_id
## <chr>
## 1 320144805
## 2 2261586826
## 3 895996433635135488
## 4 2917067949
## 5 894802788
## 6 170738644
standup4brexit_folls_data <- lookup_users(standup4brexit_folls$user_id) # Obtain the followers' profile
standup4brexit_folls_data[10,]$screen_name # Show a follower's screen name
## [1] "DonaldUnsworth"
standup4brexit_folls_data[10,]$location # location
## [1] "Wisbech Saint Mary, England"
standup4brexit_folls_data[10,]$followers_count # follower's coiunt
## [1] 455
class(standup4brexit_folls_data$location) # Check the data class of location
## [1] "character"
sort(table(standup4brexit_folls_data$location),decreasing = TRUE)[1:10] # Check the top-10 distribution of "locations"
##
## England, United Kingdom London, England
## 2003 142 137
## United Kingdom London UK
## 135 102 61
## England South East, England Wales, United Kingdom
## 38 32 22
## North West, England
## 20
class(standup4brexit_folls_data$followers_count) # Check the data class of followers_count
## [1] "integer"
summary(standup4brexit_folls_data$followers_count) # Show its summary
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.0 12.0 63.0 751.2 288.2 128591.0
plot_ly(x = ~standup4brexit_folls_data$followers_count, type = "histogram") # Show a histogram
Finally, let’s have a look at the Twitter trend data and check the class of the returned data
loc <- trends_available()
sf <- get_trends("San Francisco") # trending topics in san francisco
ny <- get_trends("New York") # trending topics in new york
tk <- get_trends("Tokyo") # trending topics in tokyo
kr <- get_trends("Korea") # trending topics in Korea
us <- get_trends("United States") # trending topics in US
ww <- get_trends("Worldwide") # all around the world
class(ww) # Check data class - data.frame
## [1] "tbl_df" "tbl" "data.frame"
Last, check the extent to which the Twitter trend of each location is shared with the worldwide one.
sum(ww$trend %in% sf$trend)/length(ww$trend) # Check if trends@WW are trends$SF
## [1] 0.3
sum(ww$trend %in% ny$trend)/length(ww$trend) # Check if trends@WW are trends$NY
## [1] 0.3
sum(ww$trend %in% tk$trend)/length(ww$trend) # Check if trends@WW are trends$TK
## [1] 0.22
sum(ww$trend %in% kr$trend)/length(ww$trend) # Check if trends@WW are trends$KR
## [1] 0.02
sum(ww$trend %in% us$trend)/length(ww$trend) # Check if trends@WW are trends$US
## [1] 0.3
sum(ny$trend %in% sf$trend)/length(ww$trend) # Check if trends@NY are trends$SF
## [1] 0.86