I'm having trouble running a loop (including the get_friends function from rtweet) since it immediately ends up exceeding the get_friends 15 people networks limit ("Warning: Rate limit exceeded - 88"). The loop works fine when using under 15 Twitterusers, but it's supposed to work for much bigger numbers. So I think the next step is to include some kind of retryonratelimit = TRUE or Sys.sleep(60x15) function? Hopefully this should be pretty straightforward, but I can't get it to work at all. The loop looks like this:
find_f <- function(df){
F1_mean <- c()
F2_mean <- c()
anyFriends <- c()
for(i in 1:nrow(df)){
#pick user1 of dataset
user_friends <- get_friends(df$name[i])
#intersect with our candidatedata
ids_intersect = intersect(user_friends$user_id, politicians$user_id)
if(length(ids_intersect)==0){
anyFriends <- c(anyFriends, FALSE) # User has no friends in the politicians df
} else {
#assign values to user based on intersect
kandidat_friends = kandidat[kandidat$user_id %in% ids_intersect,]
F1_mean <- c(F1_mean, mean(kandidat_friends$Faktor1, na.rm=TRUE))
F2_mean <- c(F2_mean, mean(kandidat_friends$Faktor2, na.rm=TRUE))
anyFriends <- c(anyFriends, TRUE) # user has friends in the politicans dataset
}
}
df$Faktor1 <- F1_mean
df$Faktor2 <- F2_mean
df$anyFriends <- anyFriends
return(df[df$anyFriends,])
}
my_users2 <- find_f(my_users)
Background information about the context: I am trying to measure political ideology on Twitter (by using Rtweet). I now have a dataframe consisting of +100 politicians user_id's along with two ideal point scores on 'factor 1' and 'factor 2' (both factors have a range of 1-4). It looks like this (called kandidat):
Navne | Faktor 1 | Faktor 2 |
---|---|---|
"Politician1" | 3.5 | 1.0 |
"Politician2" | 2.0 | 4.0 |
Etc... | X | X |
I would then like to detect if random Twitter users follow one or more of the politicians from my dataset. If they e.g. follow two of the politicians in my dataset - "Politician1" and "Politician2" - I will then assign a mean of the two politicians ideal point scores on the two factors to the user. An example of a Twitteruser following these two politicians could then be factor 1 = (3.5+1.0)/2 = 2.25 and factor 2 = (2.0+4.0)/2 = 3.00. This is what the loop is now doing - except I can't get it to work when exceeding the rate_limit as explained above.