2016-05-30 59 views
3

的纬度和经度值我收集了一些Twitter的数据这样做:不能得到鸣叫

#connect to twitter API 
setup_twitter_oauth(consumer_key, consumer_secret, access_token, access_secret) 

#set radius and amount of requests 
N=200 # tweets to request from each query 
S=200 # radius in miles 

lats=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, function(z) ifelse(length(z)==0,NA,z)) 

data=as.data.frame(cbind(lat=rogerlat,lon=rogerlon)) 

现在我想获得所有的鸣叫有长和纬度值:

data=filter(data, !is.na(lat),!is.na(lon)) 
lonlat=select(data,lon,lat) 

但是现在我只能得到NA值....任何想法在这里出了什么问题?

+1

你可以'输入'一些'罗杰'数据框(匿名,如有必要)。我们还能如何确保Twitter甚至提供了一些经纬值? – dww

回答

5

如前所述ChrissearchTwitter不会返回LAT-长的鸣叫。您可以通过进入twitteR文档,它告诉我们,它返回一个status对象看到这一点。

状态对象

向下滚动到状态对象,你可以看到的信息,11个都包括在内,但LAT-长是不是其中之一。但是,我们并没有完全丢失,因为用户的屏幕名称被返回。

如果我们看一下用户对象,我们可以看到,用户的对象至少包括位置。

所以我能想到至少有两个可能的解决方案,这取决于你的使用情况是什么。

解决方案1:提取用户的位置

# Search for recent Trump tweets # 
tweets <- searchTwitter('Trump', lang="en",n=N,resultType="recent", 
       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:多次搜索,以有限的半径

其他的解决办法是进行一系列反复搜索,增加您的纬度和小半径的经度。这样你可以相对确定用户接近你指定的位置。

0

假设一些微博中下载,也有一些地理参考鸣叫和一些鸣叫没有地理坐标:

prod(dim(data)) > 1 & prod(dim(data)) != sum(is.na(data)) & any(is.na(data)) 
# TRUE 

让我们来模拟你的经度/纬度点之间data为简单起见。

set.seed(123) 
data <- data.frame(lon=runif(200, -77, -74), lat=runif(200, 38.9, 40.7)) 
data[sample(1:200, 10),] <- NA 

可以通过删除缺失数据的10行来选择具有经度/纬度数据的行。

data2 <- data[-which(is.na(data[, 1])), c("lon", "lat")] 
nrow(data) - nrow(data2) 
# 10 

最后一行代替了代码的最后两行。但是,请注意,只有在遗漏的地理坐标存储为NA时,此功能才有效。

1

不一定是答案,但更多的观察时间太长评论:

首先,你应该看看地理编码的输入数据的文件。使用twitteR

setup_twitter_oauth(consumer_key, consumer_secret, access_token, access_secret) 

#set radius and amount of requests 
N=200 # tweets to request from each query 
S=200 # radius in miles 

地理数据应该是这样的(纬度,经度,半径)被结构化:

geo <- '40,-75,200km' 

,然后使用名为:

roger <- searchTwitter('Roger+Federer',lang="en",n=N,resultType="recent",geocode=geo) 

然后,我就改用twListtoDF过滤:

roger <- twListToDF(roger) 

现在给你一个16个色谱柱和200个观察数据(上面设置)。

然后,您可以使用过滤:

setDT(roger) #from data.table 
roger[latitude > 38.9 & latitude < 40.7 & longitude > -77 & longitude < -74] 

这就是说(为什么这是一个观察与答案) - 它看起来好像twitteR不返回纬度和经度(这是所有NA在我返回的数据) - 我认为这是为了保护个人用户的位置。

也就是说,调整半径确实会影响结果的数量,所以代码可以以某种方式访问​​地理数据。