Analytics, Digital Marketing, Social Network

Collecting Twitter Network Data with R

R is a free software environment for statistical computing and graphics. You can download it from its official website.

In the following, we share the R codes for collecting twitter search network data. This network data is created from tweets containing #BoilerUp hashtag. We create a node for each unique Twitter user who either tweeted one of those tweets, was replied to in one of those tweets, or was mentioned in one of those tweets. These nodes were connected with edges of tweeting, replying, and mentioning. If a user X1 was replied or mentioned by another user X2, the edge between X1 and X2 represents a one way connection from X2 to X1.

First step, you need install package twitterR, igraph, reshape, reshape2 from R. The package graphTweets is not offered within R and one need download it from to your computer. Then you can install this package from R.

Second step, you need gain the authorization from twitter for your R application. Please read the article here to understand the detailed process.

Third step, run your codes in R.


#setup_twitter_oauth() function uses the httr package
# replace these value from your register on twitter
#Twitter Authentication with R

Consumer_key=”YOUR API KEY”
Consumer_secret=”YOUR API SECRET”
access_token=”YOUR ACCESS TOKEN”
access_token_secret=”YOUR ACCESS TOKEN SECRET”

#pull and parse tweets
tweets=searchTwitter(“#BoilerUp”, n=5000, lang=”en”)
edges=getEdges(data = tweets, tweets = “text”, source = “screenName”)
nodes=getNodes(edges)[,1:2], directed=TRUE, vertices=nodes)
write.graph(graph, “D:/SNAdata/boilerup.graphml”, format=”graphml”)

More information about twitterR package can be found from the following link.