Site icon R-bloggers

Analyzing Twitter Data in R – Part 1

[This article was first published on Abraham Mathew » R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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 their blog: Abraham Mathew » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.