Guilhem Faure Guilhem Faure - 1 year ago 294
R Question

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,

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



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 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");

Is it possible to do this request using dplyr?

Answer Source

You could do

tw <- searchTwitter('#twitter', n = 3) 
tw_df <- twListToDF(tw)
tw_df %>%
  rowwise() %>%
  mutate(user.location = twitteR::location(getUser(screenName))) %>%
# 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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download