使用 rtweet/igraph 从 Twitter 用户 ID 确定用户是否是彼此的朋友

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

我正在使用 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)
  }

}

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

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

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

如果编码正确,则表示 none 的用户 (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

这证实了最初的答案 - 集合中没有用户关注其他用户。