How to track Twitter unfollowers in R

July 18, 2012
By

(This article was first published on Quantitative thoughts » EN, and kindly contributed to R-bloggers)

I have Twitter account and it is relatively easy to see new followers or subscribers. However, I was looking for ways to know who are the unfollowers. I have noticed, that some (un)subscriptions happen in bulks, which made me thinking that either I tweeted some bullshit and upset bunch of people or spam bots work in sync. With that in mind I have created a simple R script, which produces Markdown and html reports about unfollowers. You can find an example of such report below.

The advantage of this script is that it does not require you to sign or share your data. You just have to install twitter package and you ready to go. Nevertheless, if you want to create Markdown report, you need to install markdown and knitr packages.

The source code and build scripts can be found here.

```{r echo=FALSE,message=FALSE}
require(twitteR)
setwd('~/git/twitTracker/')
usr=getUser("dzidorius")
tmp=sapply(usr$getFollowers(),function(x)x$screenName)
 
if(!file.exists('users.csv'))
{
  ## when file doesn't exist - take users list and add some artificial user
  write.table(c(as.character(tmp),'dzidorius'),'users.csv')
 
}
 
old_list=as.character(read.table('users.csv')$x)
users=lookupUsers(old_list[which(!(old_list %in% as.character(tmp)))])
if(length(users)==0)
{
  ## stop() doesn't work under knitr
  cat('no one left you')  
 
}
```
 
 
```{r comment=NA,echo=FALSE,message=FALSE,results='asis'}
for(i in 1:length(users))
{
cat(paste("**",users[[i]]$name," @",users[[i]]$screenName,"**", "\n===\n",sep=""))
cat(paste("![](https://api.twitter.com/1/users/profile_image?screen_name=",users[[i]]$screenName,
          "&size=bigger)",sep=''))
cat(paste("  \n**Created:** ",users[[i]]$created,
          "  \n**Spam rate:** ",round(users[[i]]$followersCount/users[[i]]$friendsCount,digits=2),
          "  \n**Activity:** " , users[[i]]$statusesCount,
          "  \n**Location:** ", users[[i]]$location,"  \n",users[[i]]$description,"  \n",
          "**Last status:** ",(users[[i]]$lastStatus$text),"\n\n",sep=""))
}
```
 
```{r comment=NA,echo=FALSE,message=FALSE,results='asis'}
write.table(as.character(tmp),'users.csv')
```

Dzidas @dzidorius


Created: 2010-02-18 13:09:29
Spam rate: 0.98
Activity: 315
Location: Luxembourg
Java, C++ and R developer & data junkie
Last status: It is rainy summer in #Luxembourg but there is a party! http://t.co/FZ7eZq6u

To leave a comment for the author, please follow the link and comment on his blog: Quantitative thoughts » EN.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.