Getting Twitter Network and Friends for Multiple Users in R

Darling, all I'm new to R and I need your help

I have a list of usernames for which I need to get all subscribers and friends and create a list of ribs and save it in CSV.file for further analysis. Problem: the list of user names that I compiled is quite large, I need to iterate over the user vector and combine the results of each user into a single file. I can do this one by one, but it is certainly better to do this automatically. Here is the code I used to create edgelist for the ONE twitter user. As I said, I intend to do the same, but for a lot of tweeter names. Assuming the connection to the Twitter API is established, I use the following packages:

library(twitteR)
library(foreign)
library(xlsx)
library(base64enc)
library(rJava)
library(devtools)
library(RCurl)
library(igraph)

Then I get user friends:

start <- getUser("@camharvey")
friends_object <- lookupUsers(start$getFriendIDs())
friends_object
friendsCount(start)

get user subscribers

followers_object <- lookupUsers(start$getFollowerIDs())
followers_object
followersCount(start)

Create a list of both objects

friends <- sapply(friends_object[1:117],name)
followers <- sapply(followers_object[1:1033],name)

relations <- merge(data.frame(User='@camharvey',followers=friends), data.frame(User=followers, followers='@camharvey'), all=TRUE)

?

+4
1

for , . edgelist. , , , . ( , lookupUsers(start$getFriendIDs()) followers_object <- lookupUsers(start$getFollowerIDs()) , , ...)

users <- c("@camharvey",etc.) #List of usernames
userrelations <- list() #Create an empty list to populate

for (i in 1:length(users)){
  start <- getUser(users[i])
  friends_object <- lookupUsers(start$getFriendIDs())
  followers_object <- lookupUsers(start$getFollowerIDs())
  friends <- sapply(1:length(friends_object), 
                    function(x) name(friends_object[[x]]))
  followers <- sapply(1:length(followers_object), 
                      function(x) name(followers_object[[x]]))
  userrelations[[i]] <- merge(data.frame(User=users[i],followers=friends),
                              data.frame(User=followers, followers=users[i]), 
                              all=TRUE)
}

. csv:

user_el <- do.call("rbind",userrelations)
write.csv(user_el, "filename.csv", row.names = F)

, . :

   user  system elapsed 
175.544   3.356 317.304 

FYI, start$getFollowerIDs() , lookupUsers(start$getFollowerIDs()), , , .

+2

Source: https://habr.com/ru/post/1662297/


All Articles