Analyzing Twitter Data in R – Part 1

February 8, 2012

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

me <- getUser(“username”)

follow = me$getFollowers()
df <-“rbind”, lapply(follow,
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))

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)

df <-“rbind”, lapply(tweets,

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


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 their blog: Abraham Mathew » R. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, 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.


Mango solutions

plotly webpage

dominolab webpage

Zero Inflated Models and Generalized Linear Mixed Models with R

Quantide: statistical consulting and training




CRC R books series

Six Sigma Online Training

Contact us if you wish to help support R-bloggers, and place your banner here.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)