2012-07-26 40 views
11

最近,Edwin Chen發佈了一個地區性使用蘇打水vs流行與可口可樂的地圖,該地圖使用地理編碼的推文創建,並在飲用的環境中處理這些詞。 http://blog.echen.me/2012/07/06/soda-vs-pop-with-twitter/如何在R的twitteR軟件包中提取tweet地理編碼

他提到,他所使用的傑夫·金特里在R.果然創建Twitter的包,很容易收集使用給定詞的鳴叫,把他們在一個數據幀:

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

數據幀(tweets.df)將包含每條推文的用戶ID,tweet文本等,但似乎不包含地理編碼。有關如何在R中獲得它的任何想法?

+0

您需要提供一個'geocode'爲'searchTwitter'使用。查看庫文檔'searchTwitter'。 – 2012-07-26 17:46:49

+1

我發現您可以將「地理編碼」和「半徑」提供給「searchTwitter」,但不會爲每個拉動的推文產生地理編碼。 – iantist 2012-07-26 18:19:44

+0

但你會有你提供的地理編碼,對吧?用更小的半徑可能會給你你需要的東西? – 2012-07-26 18:25:42

回答

2

這裏是一個玩具例子,因爲你可以提取每次通話只有100鳴叫:

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) 
} 

該解決方案可能不會太高雅了,但我能得到給定的特定地理編碼的鳴叫。

3

我一直在使用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") 

example map

有我遇到我不知道如何處理一些大的問題。首先,如代碼所示,代碼搜索15個不同的隨機生成的位置,這些位置是從美國東部最大經度的最大西部和最北部的緯度到最南部的均勻分佈生成的。這將包括不在美國的地點,比如在加拿大明尼蘇達森林湖的東邊。我想要一個函數來隨機檢查生成的位置是否在美國,如果不是,則丟棄它。更重要的是,我想搜索數千個位置,但Twitter不喜歡這樣的位置,並給我一個420 error enhance your calm。因此,最好每隔幾個小時搜索一次,然後慢慢建立數據庫並刪除重複的推文。最後,如果選擇了一個遠程熱門話題,R給出了一個錯誤,如Error in function (type, msg, asError = TRUE) : transfer closed with 43756 bytes remaining to read。我對如何解決這個問題感到有些迷惑。

+0

請工作......併發布時,它的想法......即使我需要它 – juggernaut1996 2016-08-15 19:12:17

+0

你能告訴我如何從「searchTwitter」中挖掘的推文中提取經度和緯度,那麼你可以使用[this](http://www.mapbox。com) – juggernaut1996 2016-08-15 19:23:25

+0

我收到一條錯誤消息:在doRppAPICall(「search/tweets」,n,params = params,retryOnRateLimit = retryOnRateLimit,: 需要15條推文,但API只能返回0 – Selrac 2017-02-05 14:08:47

4

地理座標代表經度和緯度座標嗎? 如果是,以下命令適用於我。

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

來源:LINK

+0

這很酷似乎工作......謝謝。 – beroe 2015-02-09 00:18:32