user6533517
user6533517

Reputation: 25

Trying to retrieve followers from a list of twitter accounts in R

I'm trying to get the followers of certain accounts with the twitteR package in R:

names_list <- c("USER1","USER2", "etc.")

twitter_account <- c()

for (i in names_list){

  url = paste("https://api.twitter.com/1.1/followers/ids.json?cursor=-1&screen_name=",names_list[i], sep = ',')
  twitter <- GET(url,sig)
  content = content(twitter)

  userz <- getUser(names_list[i])
  userz$getFollowers(names_list[i])

  }

But unfortunately it doesn't work (getting an error, but even then I'm pretty sure it won't work. I'm relatively new to R so any help would be appreciated to pull this data. Even without the twitteR package.

[ Error in if (num <= 0) stop("num must be positive") else num <- as.integer(num) : 
  missing value where TRUE/FALSE needed] 

And it would be helpful if you could explain how this can be done with retrieving the names_list from a csv file actually. But that's not really necessary.

Upvotes: 0

Views: 623

Answers (1)

Choubi
Choubi

Reputation: 680

I believe your problem comes from the getUser line.

userz <- getUser(names_list[i])

In this case, 'i' is a value in names_list as you have initialized your for loop as

for (i in names_list){

Thus, your names_list[i] is equivalent to names_list['USER1'] and NOT to names_list[1].

I think what you want is either to iterate on names and refer directly to the name:

for (name in names_list){

  url = paste("https://api.twitter.com/1.1/followers/ids.json?cursor=-1&screen_name=", name, sep = ',')
  twitter <- GET(url,sig)
  content = content(twitter)

  userz <- getUser(name)
  userz$getFollowers(name)

  }

or to iterate over an index and use indexing of names_list:

for (i in 1:length(names_list)){

  url = paste("https://api.twitter.com/1.1/followers/ids.json?cursor=-1&screen_name=",names_list[i], sep = ',')
  twitter <- GET(url,sig)
  content = content(twitter)

  userz <- getUser(names_list[i])
  userz$getFollowers(names_list[i])

  }

Upvotes: 1

Related Questions