通過一些幫助,我想出瞭如何將edgelist,aka,adjacency list轉換爲adjacency matrix.我想了解如何自動執行此操作大量邊界列表,然後將得到的鄰接矩陣放入列表中。plyr應用程序,創建每個對應於數據子集的矩陣列表
我猜plyr是做這件事的最好方法,但是如果你想告訴我如何用循環做到這一點,我會很感激。好奇的是,這些數據代表了不同學校的社交網絡。
這裏是我到目前爲止有:
# extract one school edgelist from the dataframe
aSchool <- myDF[which(myDF$school==1), c("school", "id", "x1","x2","x3","x4","x5","x6","x7","x8","x9","x10")]
# figure out unique ids
edgeColumns <- c("x1","x2","x3","x4","x5","x6","x7","x8","x9","x10")
ids <- unique(unlist(aSchool[edgeColumns]))
ids <- ids[!is.na(ids)]
# make an empty matrix
m <- matrix(0,nrow=length(ids),ncol=length(ids))
rownames(m) <- colnames(m) <- as.character(ids)
# fill in the matrix
for(col in edgeColumns){
theseEdges <- aSchool[c("id",col)]
theseEdges <- na.omit(theseEdges)
theseEdges <- apply(theseEdges,1,as.character)
theseEdges <- t(theseEdges)
m[theseEdges] <- m[theseEdges] + 1
}
for(i in 1:nrow(m)) m[i,i] <- 0
我很難理解myDF是什麼。你可以粘貼dput(myDF)的輸出,這樣我們可以重現你的數據嗎? – 2011-02-25 01:13:54