Analyzing Twitter Data in R – Part 1

February 8, 2012
By

(This article was first published on Abraham Mathew » R, and kindly contributed to R-bloggers)

I recently began using the TwitteR package in R to examine my tweeting patterns. One of my first projects was to identify each of my Twitter followers, where they were located, how many tweets they had, and then plot their location on a map using a bubble which was related to their total number of tweets. Unfortunately, I found that I was unable to plot the data on to a spatial map because I did not have the coordinates for each of my followers. While I wasn’t able to successfully complete my project, I am posting my code for acquiring the data using the TwitteR package.

library(twitteR)
me <- getUser(“username”)

follow = me$getFollowers()
df <- do.call(“rbind”, lapply(follow, as.data.frame))
str(df)
head(df, 3)

user = as.character(df$screenName)
name = as.character(df$name)
location = as.character(df$location)
followers = as.character(df$followersCount)
created = as.character(df$created)

mydf = data.frame(user=c(user), name=c(name), location=c(location),
followers=c(followers), created=c(created))
mydf

I also attempted to look at the frequency of how often I favorite a tweet that contains an #rstats hashtag. I was able to identify the tweets with #rstats, when they were created, and whether I had marked that tweet as a favorite. After going through and marking various #rstats tweets as favorites, I ran the following code. Unfortunately, I found that each R related tweet was being returned as FALSE in regards to whether I had marked as a favorite. In any case, here is my R code for this task.

tweets <- searchTwitter(‘#rstats’, n=300)
tweets

df <- do.call(“rbind”, lapply(tweets, as.data.frame))
str(df)

ndf <- data.frame(text=c(as.character(df$text)), created=c(df$created),
favorited=c(df$favorited))
head(ndf)

table(ndf$favorited)

This was my first real attempt at using the TwitteR package, and I hope to dive further into this package over the next couple weeks. I will work on some new projects and will have some code which successfully completes a particular task.

To leave a comment for the author, please follow the link and comment on his blog: Abraham Mathew » R.

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...

Tags: , ,

Comments are closed.