#connect to twitter APIsetup_twitter_oauth(consumer_key,consumer_secret,access_token,access_secret)#set radius and amount of requestsN=200 # tweets to request from each queryS=200 # radius in mileslats=c(38.9,40.7)lons=c(-77,-74)roger=do.call(rbind,lapply(1:length(lats),function(i) searchTwitter('Roger+Federer',lang="en",n=N,resultType="recent",geocode=paste (lats[i],lons[i],paste0(S,"mi"),sep=","))))
在此之后我完成了:
rogerlat=sapply(roger,function(x) as.numeric(x$getLatitude()))rogerlat=sapply(rogerlat,function(z) ifelse(length(z)==0,NA,z)) rogerlon=sapply(roger,function(x) as.numeric(x$getLongitude()))rogerlon=sapply(rogerlon,z)) data=as.data.frame(cbind(lat=rogerlat,lon=rogerlon))
现在我想获得所有具有long和lat值的推文:
data=filter(data,!is.na(lat),!is.na(lon))lonlat=select(data,lon,lat)
但是现在我只获得了NA值….对这里出了什么问题的任何想法?
解决方法 正如 Chris所述,searchTwitter不会返回推文的长度.您可以通过转到 twitteR文档来看到这一点,该文档告诉我们它返回一个状态对象.状态对象
向下滚动到状态对象,您可以看到包含11条信息,但是lat-long不是其中之一.但是,我们并没有完全丢失,因为返回了用户的屏幕名称.
如果我们查看用户对象,我们会看到用户的对象至少包含一个位置.
所以我可以考虑至少两种可能的解决方案,具体取决于您的用例.
解决方案1:提取用户的位置
# Search for recent Trump tweets #tweets <- searchTwitter('Trump',geocode='38.9,-77,50mi')# If you want,convert tweets to a data frame #tweets.df <- twListToDF(tweets)# Look up the users #users <- lookupUsers(tweets.df$screenname)# Convert users to a dataframe,look at their location#users_df <- twListToDF(users)table(users_df[1:10,'location']) ❤ Texas ❤ ALT.SEATTLE.INTERNET.UR.FACE 2 1 1 Japan Land of the Free New Orleans 1 1 1 SpringfIEld OR USA United States USA 1 1 1 # Note that these will be the users' self-reported locations,# so potentially they are not that useful
解决方案2:半径有限的多次搜索
另一种解决方案是进行一系列重复搜索,以小半径增加纬度和经度.这样,您可以相对确定用户是否接近您指定的位置.
总结以上是内存溢出为你收集整理的r – 无法得到推文的纬度和经度值全部内容,希望文章能够帮你解决r – 无法得到推文的纬度和经度值所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)