无法获取推文的纬度和经度值
Cant get lat and longitude values of tweets
我这样做收集了一些推特数据:
#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))
现在我想获取所有具有 long 和 lat 值的推文:
data=filter(data, !is.na(lat),!is.na(lon))
lonlat=select(data,lon,lat)
但现在我只得到 NA 值....想知道这里出了什么问题吗?
假设下载了一些推文,其中有一些带有地理参考的推文和一些没有地理坐标的推文:
prod(dim(data)) > 1 & prod(dim(data)) != sum(is.na(data)) & any(is.na(data))
# TRUE
为了简单起见,让我们在 longitude/latitude 点之间模拟 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 行来选择具有 longitude/latitude 数据的行。
data2 <- data[-which(is.na(data[, 1])), c("lon", "lat")]
nrow(data) - nrow(data2)
# 10
最后一行替换了代码的最后两行。但是,请注意,这仅在缺少的地理坐标存储为 NA
时才有效。
不一定是答案,但更多的是观察太长无法评论:
首先,您应该查看有关如何输入地理编码数据的文档。使用 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)
现在给你一个 data.frame 有 16 列和 200 个观测值(上面设置)。
然后您可以使用以下方式进行过滤:
setDT(roger) #from data.table
roger[latitude > 38.9 & latitude < 40.7 & longitude > -77 & longitude < -74]
就是说(以及为什么这是一个观察与一个答案)- 看起来 twitteR
没有 return 纬度和经度(它在数据中都是 NA returned) - 我认为这是为了保护个人用户的位置。
也就是说,调整半径确实会影响结果的数量,因此代码确实可以以某种方式访问地理数据。
作为 Chris mentioned, searchTwitter
does not return the lat-long of a tweet. You can see this by going to the twitteR 文档,它告诉我们它 returns 是一个 status
对象。
状态对象
向下滚动到状态对象,您可以看到包含 11 条信息,但经纬度不是其中之一。但是,我们并没有完全迷路,因为返回了用户的屏幕名称。
如果我们查看用户对象,我们会发现用户的对象至少包含一个位置。
所以我至少可以想到两种可能的解决方案,具体取决于您的用例。
解决方案 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:有限半径的多次搜索
另一种解决方案是进行一系列重复搜索,以较小的半径增加经纬度。这样你就可以相对确定用户就在你指定的位置附近。
我这样做收集了一些推特数据:
#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))
现在我想获取所有具有 long 和 lat 值的推文:
data=filter(data, !is.na(lat),!is.na(lon))
lonlat=select(data,lon,lat)
但现在我只得到 NA 值....想知道这里出了什么问题吗?
假设下载了一些推文,其中有一些带有地理参考的推文和一些没有地理坐标的推文:
prod(dim(data)) > 1 & prod(dim(data)) != sum(is.na(data)) & any(is.na(data))
# TRUE
为了简单起见,让我们在 longitude/latitude 点之间模拟 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 行来选择具有 longitude/latitude 数据的行。
data2 <- data[-which(is.na(data[, 1])), c("lon", "lat")]
nrow(data) - nrow(data2)
# 10
最后一行替换了代码的最后两行。但是,请注意,这仅在缺少的地理坐标存储为 NA
时才有效。
不一定是答案,但更多的是观察太长无法评论:
首先,您应该查看有关如何输入地理编码数据的文档。使用 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)
现在给你一个 data.frame 有 16 列和 200 个观测值(上面设置)。
然后您可以使用以下方式进行过滤:
setDT(roger) #from data.table
roger[latitude > 38.9 & latitude < 40.7 & longitude > -77 & longitude < -74]
就是说(以及为什么这是一个观察与一个答案)- 看起来 twitteR
没有 return 纬度和经度(它在数据中都是 NA returned) - 我认为这是为了保护个人用户的位置。
也就是说,调整半径确实会影响结果的数量,因此代码确实可以以某种方式访问地理数据。
作为 Chris mentioned, searchTwitter
does not return the lat-long of a tweet. You can see this by going to the twitteR 文档,它告诉我们它 returns 是一个 status
对象。
状态对象
向下滚动到状态对象,您可以看到包含 11 条信息,但经纬度不是其中之一。但是,我们并没有完全迷路,因为返回了用户的屏幕名称。
如果我们查看用户对象,我们会发现用户的对象至少包含一个位置。
所以我至少可以想到两种可能的解决方案,具体取决于您的用例。
解决方案 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:有限半径的多次搜索
另一种解决方案是进行一系列重复搜索,以较小的半径增加经纬度。这样你就可以相对确定用户就在你指定的位置附近。