2

I'm sure everyone is aware of the limitations with Twitter's API and the inability to get the number of replies a given tweet has.

I'm after this information, and am trying to loop over all the followers for a given account and look back at all the times they've tweeted, and if these tweets go to the original account.

I believe this will get around the issue of only being able to get the last weeks worth of tweets, which using the search function does.

Code below doesn't work, any help would be very useful! I know Python can do this, but I'd like an R solution if possible.

library(rtweet)
library(plyr)
library(dplyr)

##set name of tweeter to look at (this can be changed)
targettwittername <- "realDonaldTrump"

##get this tweeter's timeline
tmls <- get_timeline(targettwittername, n=3200, retryonratelimit=TRUE)
##get their user id
targettwitteruserid <- as.numeric(select(lookup_users(targettwittername), user_id))
##get ids of their tweets
tweetids <- select(tmls, status_id)
tweetids <- transform(tweetids, status_id_num=as.numeric(status_id))

##get list of followers (who are most likely to reply)
targetfollowers <- get_followers(targettwittername, retryonratelimit=TRUE)



master_reply_counts <-data.frame(reply_to_status_id_num=integer(),n=integer())




getfollowersreplies <- function(follower){
  follower <- as.numeric(filter(targetfollowers, user_id==follower))
  followertl <- get_timeline(user = follower, n=3200, retryonratelimit=TRUE)
  followertl <- filter(followertl, reply_to_user_id == targettwitteruserid)
  followertl <- transform(followertl, reply_to_status_id_num=as.numeric(reply_to_status_id))
  join <- inner_join(followertl, tweetids, by=c("reply_to_status_id_num"="status_id_num"))
  replycounts <- join %>%
                  group_by(reply_to_status_id_num) %>%
                  summarise(n=n())
  master_reply_counts <- rbind(master_reply_counts, replycounts)
  return(follower)
}

data <- ldply(targetfollowers$user_id, getfollowersreplies)
Peter
  • 107
  • 1
  • 9

0 Answers0