在R中的有向网络中查找匹配项
我正在研究一个使用iGraph的定向数据集,该数据集基于在Twitter上相互关注的人。我有一个数据表在R中的有向网络中查找匹配项,r,datatable,match,igraph,reshape2,R,Datatable,Match,Igraph,Reshape2,我正在研究一个使用iGraph的定向数据集,该数据集基于在Twitter上相互关注的人。我有一个数据表 follower_user_id | followed_user_id | gender_of_follower | gender_of_followed 1 | 2 | F | M 2 | 3 |
follower_user_id | followed_user_id | gender_of_follower | gender_of_followed
1 | 2 | F | M
2 | 3 | M | M
3 | 2 | M | M
等等
我想评估匹配情况,用户在哪些情况下相互跟踪,以便我可以进一步了解谁没有被跟踪,例如男性是否比女性更可能被跟踪(等等)。但我不知道首先如何筛选比赛
到目前为止,我认为最好的方法是使用一个包含所有用户ID和所有用户ID的矩阵,并计算每一对出现在一起的时间
M <- table (df$follower_user_id, df$followed_user_id)
follower.followed.matrix <- M %*% t(M)
XXXXX 1 2 3
1 0 1 0
2 0 0 1
3 0 1 0
M获取所需矩阵-邻接矩阵-内置于igraph中。我将使用一个比您稍大的示例来确保解决方案能够处理所有情况
数据:扩大的跟随者网络
现在,通过邻接矩阵,你可以快速计算A跟在B后面,B跟在A后面的对
sapply(1:5, function(x) { AM[x,] * AM[,x] })
[,1] [,2] [,3] [,4] [,5]
[1,] 0 0 0 0 0
[2,] 0 0 1 1 0
[3,] 0 1 0 0 0
[4,] 0 1 0 0 0
[5,] 0 0 0 0 0
您可以看到所需的对是(2,3)、(2,4)、(3,2)和(4,2)
sapply(1:5, function(x) { AM[x,] * AM[,x] })
[,1] [,2] [,3] [,4] [,5]
[1,] 0 0 0 0 0
[2,] 0 0 1 1 0
[3,] 0 1 0 0 0
[4,] 0 1 0 0 0
[5,] 0 0 0 0 0