我试图找到一种更快的方法来总结会话中个体的出现,以计算他们的关系水平。基本上,我想为每两个人计算至少观察到其中一个人的次数。我在这里查看了包括melt 和acast 在内的答案,但没有找到使用它们的好方法。这是我效率低下的代码:
presence=array(0,c(8,10)) # session by individual
counter=array(0,c(10,10))
# put some data into the array:
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 0 1 0 0 0 0 0 0 0 0
[2,] 0 0 1 1 1 1 0 0 0 0
[3,] 0 0 0 0 0 0 1 0 0 0
[4,] 0 0 0 0 0 0 0 1 0 0
[5,] 0 0 0 0 0 0 0 1 0 0
[6,] 0 0 0 0 0 0 0 0 1 0
[7,] 0 0 0 0 0 0 0 0 1 0
[8,] 0 0 1 0 1 1 0 0 0 1
for (a in 1:10){
for (b in 1:10){
for (c in 1:8) {
if ((presence[c,a]>0)|(presence[c,b]>0)) counter[a,b]=counter[a,b]+1
# calculate something using this counter for each two individuals a and b
}
}
}
谢谢您的帮助。阿米亚尔