jeangelj
jeangelj

Reputation: 4498

Plot Count/frequency of tweets for word by month

I have connected R to Twitter and am scraping using the searchTwitter function in R, and cleaning the resultant data for punctuation, lowercase, etc. Now I'm trying to do the following:

I'd like to reuse this for retweets, mentions, replies and favorites.

This is what I've tried so far:

#load the packages into R
>library(twitteR)
>library(plyr)
>library(ggplot2)    

# Register an application (API) at https://apps.twitter.com/
# Look up the API key and create a token – you need for both the key and the secret
# Assign the keys to variables and use the authorization
api_key <- “your API key from twitter”
api_secret <- “your Secret key from twitter”
access_token <- “you Access Token from twitter”
access_token_secret <- “you Access Token Secret key from twitter”
setup_twitter_oauth(api_key,api_secret,access_token,access_token_secret)

1 "Using direct authentication" Use a local file to cache OAuth access credentials between R sessions?
1: Yes
2: No
# Type 1 and press Enter
Selection: 1

auctiontweets <- searchTwitter("auction", since = "2015-01-01", until = "2015-08-03", n=1000)

However, I'm having trouble creating a data frame, getting the following error:

tweet.dataframe <- data.frame(searchTwitter("action", since = "2015-01-01", until = "2015-08-03", n=3000))

Error in as.data.frame.default(x[[i]], optional = TRUE) :
cannot coerce class "structure("status", package = "twitteR")" to a data.frame

I found code on how to plot users by hour; but wasn't able to modify it so that it works for tweets with a specific word (i.e. 'auction') per month:

yultweets <- searchTwitter("#accessyul", n=1500)
y <- twListToDF(yultweets)
y$created <- as.POSIXct(format(y$created, tz="America/Montreal"))
yply <- ddply(y, .var = "screenName", .fun = function(x) {return(subset(x,     
created %in% min(created), select = c(screenName,created)))})
yplytime <- arrange(yply,-desc(created))
y$screenName=factor(y$screenName, levels = yplytime$screenName)
ggplot(y) + geom_point(aes(x=created,y=screenName)) + ylab("Twitter username") + xlab("Time")

The source can be found here.

Upvotes: 3

Views: 2663

Answers (1)

daniel
daniel

Reputation: 1246

Since you didn't provide even a small piece of your data that we could cope with, my answer might superficial.

library(stringi); library(dplyr); library(SciencesPo)

  df <- data.frame(tweets = c("blah, blah, Blah, auction","blah, auction", "blah, blah", "this auction, blah", "today"), date=c('2015-07-01','2015-06-01','2015-05-01','2015-07-31','2015-05-01'))
  > df
                         tweets       date
    1 blah, blah, Blah, auction 2015-07-01
    2             blah, auction 2015-06-01
    3                blah, blah 2015-05-01
    4        this auction, blah 2015-07-31
    5                     today 2015-05-01

 filter = "auction"

> df$n <- vapply(df$tweets, function(x) sum(stri_count_fixed(x, filter)), 1L)
> df
                     tweets       date n
1 blah, blah, Blah, auction 2015-07-01 1
2             blah, auction 2015-06-01 1
3                blah, blah 2015-05-01 0
4        this auction, blah 2015-07-31 1
5                     today 2015-05-01 0

Then, the only thing is to summarise:

 df %>% group_by(month=format(as.Date(date),format="%m")) %>% summarize(freq=sum(n)) 
%>%ungroup() -> df2

> df2
    Source: local data frame [3 x 2]

      month freq
    1    05 0
    2    06 1
    3    07 2
    > 

Voilà! Bonus, plot it as ggplot(df2, aes(x=month, y=freq)) + geom_line() + theme_pub()

Upvotes: 1

Related Questions