Guilhem Faure
Guilhem Faure

Reputation: 53

Get locations of user from a tweet search with twitteR and dplyr

I would like to obtain the locations of user from a searchTwitter results using dplyr.

First, I search for tweets containing a specific tag and I convert them in the dplyr framework:

tw = searchTwitter('#twitter', n = 100, since = '2012-01-01') 
tw_df <- tbl_df(map_df(tw, as.data.frame))

Then I want to extract the user to get their locations. I am using the getUser() and the location() function:

e.g.

location(getUser('testusername'))

However, when I want to combine this in a dplyr pipeline, I got an error:

tw_df %>%
  mutate(user.location = location(getUser(screenName)))

error:

Error: error in evaluating the argument 'object' in selecting a method for function 'location': Error in twInterfaceObj$doAPICall(paste("users", "show", sep = "/"), params = params,  : 
  Not Found (HTTP 404).

I thought it could be a restriction of twitter multiple requests, however, outside of dplyr pipeline, it works:

for (i in 1:10) {
    test.user <- getUser("testusername");
    print(location(test.user))
}

Is it possible to do this request using dplyr?

Upvotes: 1

Views: 287

Answers (1)

lukeA
lukeA

Reputation: 54237

You could do

library(twitteR)
library(dplyr)
tw <- searchTwitter('#twitter', n = 3) 
tw_df <- twListToDF(tw)
tw_df %>%
  rowwise() %>%
  mutate(user.location = twitteR::location(getUser(screenName))) %>%
  select(user.location)
# Source: local data frame [3 x 1]
# Groups: <by row>
# 
# # A tibble: 3 x 1
#                   user.location
#                           <chr>
# 1                              
# 2 En la VI Republica, Venezuela
# 3                 San Mateo, CA

tw_df %>% mutate(user.location = location(getUser(screenName))) feeds screenName with all screen names at once. That does not work here.

Upvotes: 1

Related Questions