【问题标题】:Determine if users are friends to one another from twitter user ids using rtweet/igraph使用 rtweet/igraph 从 twitter 用户 ID 确定用户是否是彼此的朋友
【发布时间】:2020-09-25 20:09:25
【问题描述】:

我正在使用 twitter 数据进行机器学习,在尝试查找用户之间的关系时,我卡在了 R 中 rtweet 包中的 lookup_friendships。 实际上我有一个用户 ID 列表,例如:-

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

有什么方法可以确定以上用户是否是彼此的朋友。我尝试使用 get_friendships() 但它提供了大约 2000 行的数据框,并且通过它们查找友谊非常耗时。

    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)
  }

}

有没有其他方法可以确定有多少用户是彼此的朋友。如果能回复就太好了。 提前致谢 问候

【问题讨论】:

    标签: r twitter igraph rtweet


    【解决方案1】:

    此答案查看您当前的数据集并检查用户之间是否存在任何关系以及他们关注的人(rtweet 包术语中的友谊)。没有找到。您可能想与一些您知道的相关用户进行核对。

    # 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>
    

    如果编码正确,则表明没有用户 (user) 关注其他用户。我在下面用不同的方式检查了这个......

    # 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
    

    这证实了原始答案 - 集合中没有用户关注其他用户。

    【讨论】:

      猜你喜欢
      • 2012-02-14
      • 1970-01-01
      • 1970-01-01
      • 2014-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-14
      • 1970-01-01
      相关资源
      最近更新 更多