Twitter friends and (un-)followers with R

A few weeks ago  the number of my twitter followers exceeded 100 and was going slightly down again and I was interested in who unfollowed me. Twitter does not offer that and I would have had to register at a third party page, which even wanted write access to my twitter account.  I did not think about it for a while and then I found the R package twitteR, which lets you fetch all required information and I used it in the following to evaluate my “friends” and “(un-)followers”.

The following code compares the “friends” (people I follow) and my “followers”. I do not really like the term “friends” for those people/organizations I follow, since I don’t know most of them. Nevertheless, I think it is interesting what they tweet.

To make use of the twitteR library I needed to obtain a consumer and access token key/secret pair from apps.twitter.com by creating an application, which I could then use in my R code. The setup_twitter_oauth() asks in an interactive session, if the credential should be stored and shared between R sessions. First initialize the session:

library(twitteR)

## insert the keys derived at apps.twitter.com
consumer.key <- "<consumer key>"
consumer.secret <- "<consumer secret>"
access.token <- "<access token>"
access.token.secret <- "<access token secret>"

setup_twitter_oauth(consumer.key, consumer.secret, access.token, access.token.secret)

Then get the information about me, my friends and followers:

me <- getUser("JoergSteinkamp")
followers <- me$getFollowers()
friends <- me$getFriends()

Look at the differences: who is not followed back by me and those who do not follow me back:

message("Followers I do not follow back:")
print(setdiff(twListToDF(followers)$screenName, twListToDF(friends)$screenName))
message("Friends not following back:")
print(setdiff(twListToDF(friends)$screenName, twListToDF(followers)$screenName))

To track new followers through R and track the unfollowers, I need at least two snapshots of my follower list. Therefore I saved one list. And once I realized, that I had a follower less, I ran the script a second time with the following addition and compared the old with the new follower list.

save(file=paste("/path/to/twitter_",format(Sys.time(), "%Y%m%d%H%M%S"),".RData", sep=""), followers, friends)

### load the 2 most recent historic file
files <- list.files("/path/to/", sep=""), pattern="^twitter_[0-9]*.RData$", full.names=TRUE)

if (length(files) <= 1) {
warning("Need at least two historic files for comparison")
q()
}
load(files[length(files)-1])
followers.old <- twListToDF(followers)$screenName
load(files[length(files)])
followers.new <- twListToDF(followers)$screenName

new.followers <- setdiff(followers.new, followers.old)
unfollowers <- setdiff(followers.old, followers.new)

message("New followers:")
print(new.followers)
message("Unfollowers:")
print(unfollowers)

I realized that most unfollowers were people following a few thousands other accounts, and when they realized I do not follow them back, they stopped following me. I must say, that I don’t want to follow others only to increase my number of followers, but because I think the other twitter feed might be interesting or funny.

Advertisements

One thought on “Twitter friends and (un-)followers with R

  1. Pingback: My “twitter bubble” R analysis – Jörg Steinkamps Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s