基于共同贡献创建加权网络

时间:2016-01-12 03:00:31

标签: r

我有以下数据集:

test <- data.frame(V1 = c("A","B","C","A","B","C","A","A"),
                     V3 = c("X","X","X","X","Y","Y","Y","Y"),
                     V2 = c(0,0,0,0,0,0,0,0))
atest <- aggregate(V2~V1+V3,data=test,FUN=length)

to = unique(atest$V3)

el <- data.frame (a = NA, b = NA, weight = NA)
for (y in (1:length(to))) {
  curto <- to[y]
  curpo <- atest[atest$V3 == curto,]
  result <- expand.grid(a=unique(curpo$V1),b=unique(curpo$V1))
  result <- result[result$a != result$b,]
  curpo$V3 <- NULL
  result <- merge(result,curpo,by.x=c("a"),by.y=c("V1"),all.x=T)
  if (length(result)==0) {
    result = data.frame (a = NA, b = NA, weight = NA)
  } else {
    result = data.frame(a = result[,1], b = result[,2], weight = result[,3])
  }
  el = rbind(el,result)
  cat(paste(y,"\r\n"))
}

el <- na.omit(el)
el <- aggregate(weight~a+b,data=el,FUN=sum)

我想根据人们在X和Y中的活动来构建一个网络。我可以使用for循环查看V3上的每组项目,但这样做会很慢。 V2列仅用于聚合函数,它没有其他用途。我想得到的结果应该是这样的:

  A - B - 4
  A - C - 4
  B - C - 2
  B - A - 2
  C - A - 2
  C - B - 2

0 个答案:

没有答案
相关问题