Search code examples
rtwitterigraphrtweet

Determine if users are friends to one another from twitter user ids using rtweet/igraph


I am using twitter data for machine learning and while trying to find relations between users i got stuck at lookup_friendships from the rtweet package in R. Actually i have a list of user IDs like:-

frnd_20$user_id
<chr>
818910970567344128              
52544275                
39344374                
41634520                
22203756                
39349894                
50769180                
22703645                
471672239               
23970102

is there any way to determine whether any of the above users is friends to each other. I tried using get_friendships() but it gave a dataframe of around 2000 rows and looking through them to find friendship is quite time consuming.

    df <- c()
for ( i in 1:20) {
  source_frnd <- frnd_20$screen_name[i]
  for(j in 1:20){
    target_frnd <- frnd_20$screen_name[j]
    a<-lookup_friendships(source_frnd,target_frnd)
    df <- rbind(df,a)
  }

}

is there any other way to determine how many users are friends to each other. Would be really great if you reply. Thanks in advance regards


Solution

  • This answer looks at your current data set and checks if there are any relations among the users and who they follow (friendships in rtweet package terminology). None are found. You might want to check it with some users you know are related.

    # reading in the data provided
    a <- "
    818910970567344128              
    52544275                
    39344374                
    41634520                
    22203756                
    39349894                
    50769180                
    22703645                
    471672239               
    23970102 "
    
    df <- read.table(text = a, header = FALSE)
    names(df) <- "targ_users"
    df$targ_users <- as.character(df$targ_users)
    
    # getting followed accounts with the rtweet function `get_friends`
    frnds <- get_friends(users = df$targ_users)
    
    library(dplyr)
    frnds %>% group_by(user) %>% summarise(n = n())
    # A tibble: 10 x 2
       user                   n
       <chr>              <int>
     1 22203756              40
     2 22703645              97
     3 23970102              76
     4 39344374            1463
     5 39349894             926
     6 41634520               4
     7 471672239            832
     8 50769180             343
     9 52544275            1448
    10 818910970567344128    15
    
    frnds %>% group_by(user) %>% filter(user %in% user_id)
    # A tibble: 0 x 2
    # Groups:   user [0]
    # ... with 2 variables: user <chr>, user_id <chr>
    

    If the coding is correct, then this indicates none of the users (user) are following the other users. I checked this a different way below...

    # make data frame of just the users
    u_df <- as.data.frame(unique(frnds$user)) 
    
    # make data frame of all the followed users
    f_df <- as.data.frame(unique(frnds$user_id))
    
    # check if any common id's are in the two groups
    u_df %in% f_df
    [1] FALSE
    

    This confirms the original answer - no users in the set follow the other users.