按月为单词绘制 Count/frequency 条推文

Plot Count/frequency of tweets for word by month

我已将 R 连接到 Twitter,并正在使用 R 中的 searchTwitter 函数进行抓取,并清理标点符号、小写等的结果数据。现在我正在尝试执行以下操作:

我想将其重复用于转发提及回复最爱.

这是我目前尝试过的方法:

#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)

但是,我在创建数据框时遇到问题,出现以下错误:

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

我找到了如何按小时绘制用户的代码;但无法对其进行修改,使其适用于每月具有特定单词(即 'auction')的推文:

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")

来源可以找到here

由于您甚至没有提供我们可以处理的一小部分数据,所以我的回答可能很肤浅。

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

那么,唯一的总结就是:

 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
    > 

瞧!奖金,将其绘制为 ggplot(df2, aes(x=month, y=freq)) + geom_line() + theme_pub()