我正在使用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
软件包术语中的友谊)。没有找到。您可能需要与认识的一些用户进行检查。
# 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
这将确认原始答案-集合中没有用户跟随其他用户。