2016-12-04 91 views
1

我想使用twitteR運行簡單搜索,但只返回位於美國的推文我知道twitteR在緯度/長度內具有lat/long和miles的地理編碼參數,但是這種定位方式整個國家的推文似乎很難。twitteR搜索R中的地理編碼參數

我會輸入什麼參數才能獲得美國推文?

感謝,

回答

0

我做了一個簡單的搜索四周,它看起來像Twitter並沒有內置的國家的說法。但是由於你有lat/long,對美國國家shapefile進行空間連接非常簡單(即以多邊形爲單位)。

在這個例子中,我使用了shapefile from Census.gov和包裝spatialEcopoint.in.polygon()功能。與其他軟件包提供的功能相比,它是一種非常快速的空間連接功能,即使您擁有數十萬個座標和數十個多邊形。如果您有數百萬條推文 - 或者您稍後決定加入多個多邊形,例如所有的世界國家 - 那麼它可能會慢很多。但對於大多數目的而言,速度非常快。

(另外,我沒有一個Twitter的API建立,所以我打算使用與tweet_ids的示例數據幀和經/緯)

library(maptools) # to 
library(spatialEco) 

# First, use setwd() to set working directory to the folder called cb_2015_us_nation_20m 
us <- readShapePoly(fn = "cb_2015_us_nation_20m") 
# Alternatively, you can use file.choose() and choose the .shp file like so: 
us <- readShapePoly(file.choose()) 

# Create data frame with sample tweets 
# Btw, tweet_id 1 is St. Louis, 2 is Toronto, 3 is ouston 
tweets <- data.frame(tweet_id = c(1, 2, 3), 
       latitude = c(38.610543, 43.653226, 29.760427), 
       longitude = c(-90.337189, -79.383184, -95.369803)) 

# Use point.in.poly to keep only tweets that are in the US 
coordinates(tweets) <- ~longitude+latitude 
tweets_in_us <- point.in.poly(tweets, us) 
tweets_in_us <- as.data.frame(tweets_in_us) 

現在,如果你看一下tweets_in_us你應該只看到經緯度在美國地區的經緯度。