Saturday, November 26, 2011

Analytics using R: Most active in my Twitter list

I follow some 80 odd people/ news sources on my twitter account. For a while I wondered which of these sources are most active on twitter. I picked a simple metric '# of status messages posted to twitter' as the measure of activity. Using R I quickly wrote a program to generate my top 10 most active twitter sources.

Here is the bar plot of the result
As expected news sources dominate the list. Among individuals "Michael Hyatt" and "Jurgen Appelo" are most active. 

If you are interested in 'R', here is the code to extract this report:

## Prerequisite: Install twitteR package 'install.packages(twitteR)
## load twitteR package
library(twitteR)

##get handle to a twitteR user object (in this case for user d_lalit
tuser <- getUser('d_lalit')

##get list of friends of d_lalit
tfriends <- userFriends(tuser)

##create an array to store the name and number of status messages for each friend
friendsCount <- length(tfriends)
friendsName <- character(friendsCount)
friendsMsgCount <- numeric(friendsCount)

for (i in 1:friendsCount) {
  friendsName[i] <- tfriends[[i]]$screenName
  friendsMsgCount[i] <- as.numeric(tfriends[[i]]$statusesCount)
}

## prepare a sortedlist and extract top 10 values from the list
sortedlist <- sort(friendsMsgCount, index.return = TRUE, decreasing=TRUE)
top10friendsName <- character(10)
top10friendsMsgCount <- numeric(10)

for (i in 1:10) {
  top10friendsName[i] <- friendsName[sortedlist$ix[[i]]] ## index is stored under ix
  top10friendsMsgCount[i] <- as.numeric(sortedlist$x[[i]])
}

## plot the chart
barplot(top10friendsMsgCount, width = 0.25, names.arg = top10friendsName, horiz=FALSE, main="Twitter friends by activity count", ylab="Number of status messages", xlab="twitter friends", space=0.2, density=50, angle=45, cex.names=0.7) 
  
I realize the code is not optimally written. Any suggestions refine the code will be appreciated.

Update: 11/29/2011
In the latest version of twitteR package, the method userFriends() has been deprecated. You may replace line#9 in the above code as with the code given below:

tfriends <- tuser$getFriends()

No comments: