我已将R连接到Twitter并使用R中的searchTwitter
函数进行抓取,并清除结果数据以标点符号,小写等。现在我尝试执行以下操作:
x-axis - month
; y-axis - number of tweets
)中绘制计数。我想重复使用转推,提及,回复和收藏。< / p>
这是我迄今为止所尝试过的:
#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&#34;使用直接身份验证&#34; 使用本地文件在R会话之间缓存OAuth访问凭据? 1:是
2:没有 #键入1并按Enter键 选择: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))
as.data.frame.default(x [[i]],可选= TRUE)出错:
不能强迫阶级结构(&#34;状态&#34;,包=&#34; twitteR&#34;)&#34;到data.frame
我找到了关于如何按小时绘制用户的代码;但是无法对其进行修改,以便它适用于每个月有特定字词(即“拍卖”)的推文:
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。
答案 0 :(得分:1)
由于您甚至没有提供我们可以处理的一小部分数据,我的回答可能是肤浅的。
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()
瞧!奖金,将其标示为accepts_nested_attributes_for