如何在R中提取twitteR包中的tweet地理编码

如何在R中提取twitteR包中的tweet地理编码,r,twitter,geocode,R,Twitter,Geocode,最近,Edwin Chen发布了一张关于苏打水、汽水和可乐的地区使用情况的地图,这张地图是根据地理编码的推特创建的,这些推特将这些词与饮酒相关 他提到他使用了Jeff Gentry在R中创建的twitteR软件包。果然,很容易收集使用给定单词的推文并将其放入数据框中: require(twitteR) require(plyr) cat.tweets<-searchTwitter("cats",n=1000) tweets.df = ldply(cat.tweets, function(t

最近,Edwin Chen发布了一张关于苏打水、汽水和可乐的地区使用情况的地图,这张地图是根据地理编码的推特创建的,这些推特将这些词与饮酒相关

他提到他使用了Jeff Gentry在R中创建的twitteR软件包。果然,很容易收集使用给定单词的推文并将其放入数据框中:

require(twitteR)
require(plyr)
cat.tweets<-searchTwitter("cats",n=1000)
tweets.df = ldply(cat.tweets, function(t) t$toDataFrame() ) 
require(推特)
需要(plyr)

cat.tweets这里是一个玩具示例,假设您每次通话只能提取100条tweets:

require(twitteR)
require(plyr)
URL = paste('http://search.twitter.com/search.atom? 
      q=','&geocode=39.724089,-104.820557,3mi','&rpp=100&page=', page, sep='') #Aurora,CO with radii of 3mi
XML = htmlTreeParse(URL, useInternal=TRUE)
entry = getNodeSet(XML, "//entry")
tweets = c()

for (i in 1:99){ 
    t = unlist(xpathApply(entry[[i]], "//title", xmlValue))
    tweets = c(tweets,t)
}

这个解决方案可能不太优雅,但我能够在特定的地理编码下获得tweet。

我一直在修补R函数,输入搜索文本、搜索站点数量和每个站点周围的半径。例如
twitterMap(“#rstats”,10,“10mi”)
下面是代码:

twitterMap <- function(searchtext,locations,radius){
require(ggplot2)
require(maps)
require(twitteR)
#radius from randomly chosen location
radius=radius
lat<-runif(n=locations,min=24.446667, max=49.384472)
long<-runif(n=locations,min=-124.733056, max=-66.949778)
#generate data fram with random longitude, latitude and chosen radius
coordinates<-as.data.frame(cbind(lat,long,radius))
coordinates$lat<-lat
coordinates$long<-long
#create a string of the lat, long, and radius for entry into searchTwitter()
for(i in 1:length(coordinates$lat)){
coordinates$search.twitter.entry[i]<-toString(c(coordinates$lat[i],
coordinates$long[i],radius))
}
# take out spaces in the string
coordinates$search.twitter.entry<-gsub(" ","", coordinates$search.twitter.entry ,
fixed=TRUE)

#Search twitter at each location, check how many tweets and put into dataframe
for(i in 1:length(coordinates$lat)){
coordinates$number.of.tweets[i]<-
 length(searchTwitter(searchString=searchtext,n=1000,geocode=coordinates$search.twitter.entry[i]))
}
#making the US map
all_states <- map_data("state")
#plot all points on the map
p <- ggplot()
p <- p + geom_polygon( data=all_states, aes(x=long, y=lat, group = group),colour="grey",     fill=NA )

p<-p + geom_point( data=coordinates, aes(x=long, y=lat,color=number.of.tweets
                                     )) + scale_size(name="# of tweets")
p
}
# Example
searchTwitter("dolphin",15,"10mi")

twitterMap地理编码是指经纬度坐标吗?
如果是,以下命令适用于我

cat.tweets = searchTwitter("cats",n=1000)
tweets.df = do.call("rbind",lapply(cat.tweets,as.data.frame))

来源:

您需要提供一个
geocode
searchTwitter
使用。请参阅图书馆文档
?searchTwitter
。我看到您可以在
searchTwitter
中提供地理编码和半径,但这不会为每个拉入的推文生成地理编码。但您会拥有您提供的地理编码,对吗?半径越小,你需要什么?好主意,我明白你的意思。我基本上可以在给定地图上迭代一个点网格。谢谢你的建议。当你开始工作的时候,你应该回答你自己的问题,这样别人就能看到你是怎么做的。我真的很喜欢你链接到的帖子,但是他们没有发布任何代码=(请处理它…当它被计算出来后发布…即使我需要它,你能告诉我如何从
searchTwitter
的推文中提取经度和纬度吗?然后你可以使用我收到一条错误消息:在Dorpapacall(“搜索/推文”),n,params=params,retryOnRateLimit=retryOnRateLimit,:请求了15条推文,但API只能返回0