Analyzing Social Media Data in R
Sowmya Vivek
Data Science Coach
screen_name
stores the twitter handle of a userrtweet
library# Extract tweets on "#brexit" using search_tweets()
tweets_df <- search_tweets("#brexit")
# View the column names
names(tweets_df)
screen_name
to understand user interestfollowers_count
to compare social media influenceretweet_count
and text
to identify popular tweetsscreen_name
refers to the twitter handle# Extract tweets on "#Arsenal" using search_tweets()
twts_arsnl <- search_tweets("#Arsenal", n = 18000)
# Create a table of users and tweet counts for the topic
sc_name <- table(twts_arsnl$screen_name)
head(sc_name)
_____today_____ ___JJ23 ___SAbI__ __ambell __Amzo__ __bobbysingh
1 2 3 1 1 1
# Sort the table in descending order of tweet counts
sc_name_sort <- sort(sc_name, decreasing = TRUE)
# View top 6 users and tweet frequencies
head(sc_name_sort)
_whatthesport footy90com Official_ATG1 TheShortFuse RubellM ArsenalZone_Ind
176 90 88 53 48 43
# Extract user data using lookup_users()
tvseries <- lookup_users("GameOfThrones", "fleabag", "BreakingBad")
# Create a dataframe with the columns screen_name and followers_count
user_df <- tvseries[,c("screen_name","followers_count")]
# View the followers count for comparison
user_df
screen_name followers_count
<chr> <int>
GameOfThrones 8597188
fleabag 58727
BreakingBad 1240349
retweet_count
stores number of retweets# Create a data frame of tweet text and retweet counts
rtwt <- tweets_arsenal[,c("text", "retweet_count")]
# Sort data frame based on descending order of retweet counts
library(dplyr)
rtwt_sort <- arrange(rtwt, desc(retweet_count))
# Exclude rows with duplicate tweet text
rtwt_unique <- unique(rtwt_sort, by = "text")
# Print top 6 unique posts retweeted most number of times
head(rtwt_unique)
retweet_count text
<int> <chr>
5606 Once a Gunner, Always a Gunner. We are proud of you @alexanderiwob
3764 Emirates on Fire ???????????????? Never give up Gunners???????????????????????? #Arsenal #CO
2798 That mood tonight ?????? 3?? POINTS ?????? #Arsenal #Gunners #COYG h
2741 #Arsenal fan: "I reckon we'll win the League this season." @Robbie
1687 Auba ???????????????? This is what I call happiness #aubameyang #arsenal
1166 When sky sports introduced the new Monday night football! The Sha
Analyzing Social Media Data in R