Twitter: Get followers from multiple users at the same time
I am working on a project where I need to find access to some social events. I want to know how many people were commented on at a festival called Tinderbox in Denmark. What I am doing is getting Twitter statistics including the Danish word "tinderbox". Then I want to extract the number of followers from these screens. So the first part of my code is given:
library("twitteR")
setup_twitter_oauth(consumer_key,consumer_secret,access_token,access_secret)
1
#get data
TB<-searchTwitter("tinderbox", lan="da", n=10000)
#put into a dataframe
df <- do.call("rbind", lapply(TB, as.data.frame))
My thought is to use the same result as in the example below, i.e. get followersCount directly from twitter data. An example can be found here on stackoverflow. But I don't know how to do it to solve my problem ( fetching large number of followers and followers in R )
library(twitteR)
user <- getUser("krestenb")
followers <- user$getFollowers()
b <- twListToDF(followers)
f_count <- as.data.frame(b$followersCount)
u_id <- as.data.frame(b$id)
u_sname <- as.data.frame(b$screenName)
u_name <- as.data.frame(b$name)
final_df <- cbind(u_id,u_name,u_sname,f_count)
sort_fc <- final_df[order(-f_count),]
colnames(sort_fc) <- c('id','name','s_name','fol_count')
My problem is that I can't just use the username vector in the followers <- user $ getFollowers (), extracting the list of screen names from df $ screenName.
So, I thought that maybe I need to do a loop with all the different screens. But I don't know how to do it.
I have that I painted a picture of what I want to get and how I thought / thought I could get there.
Help is greatly appreciated as the festival is due this weekend.
source to share
Here's a sample code based on what you had in the original problem that will aggregate Twitter results for a set of users:
# create a data frame with 4 columns and no rows initially
df_result <- data.frame(t(rep(NA, 4)))
names(df_result) <- c('id', 'name', 's_name', 'fol_count')
df_result <- df_result[0:0,]
# you can replace this vector with whatever set of Twitter users you want
users <- c("krestenb", "tjb25587") # tjb25587 (me) has no followers
# iterate over the vector of users and aggregate each user results
sapply(users, function(x) {
user <- getUser(x)
followers <- user$getFollowers()
if (length(followers) > 0) { # ignore users with no followers
b <- twListToDF(followers)
f_count <- as.data.frame(b$followersCount)
u_id <- as.data.frame(b$id)
u_sname <- as.data.frame(b$screenName)
u_name <- as.data.frame(b$name)
final_df <- cbind(u_id,u_name,u_sname,f_count)
sort_fc <- final_df[order(-f_count),]
colnames(sort_fc) <- c('id','name','s_name','fol_count')
df_result <<- rbind(df_result, sort_fc)
}
})
Important points
I used the global assignment operator <<-
when executing rbind
in the data frame df_result
to get it out of the loop. As I mentioned in my original answer, you can use a function sapply
to iterate over a vector of users. Within the loop, the results are aggregated.
I tested a vector containing Twitter users with and without followers and it worked.
source to share