为data.frame中的所有交互创建边缘列表 [英] Create edgelist for all interactions from data.frame
问题描述
我正在尝试在 igraph
中进行网络分析,但是在给定的列数不同的情况下,将我拥有的数据集转换为边缘列表(具有权重)存在一些问题.
I am trying to do network analysis in igraph
but having some issues with transforming the dataset I have into an edge list (with weights), given the differing amount of columns.
数据集如下所示( df1
)(当然要大得多):首先是主操作员ID(主操作员也可以是伙伴,反之亦然,因此ID保持不变挑战在于合作伙伴的数量各不相同(从0到40),并且必须考虑每个交互(而不仅仅是"IdMain到IdPartnerX").
The data set looks as follows (df1
) (much larger of course): First is the main operator id (main operator can also be partner and vice versa, so the Ids are staying the same in the edge list) The challenge is that the amount of partners varies (from 0 to 40) and every interaction has to be considered (not just "IdMain to IdPartnerX").
IdMain IdPartner1 IdPartner2 IdPartner3 IdPartner4 .....
1 4 3 7 6
2 3 1 NA NA
3 1 4 2 NA
4 9 6 3 NA
.
.
我已经获得了有用的技巧,可以使用重塑来做到这一点,例如:
I already got the helpful tip to use reshape to do this, like:
data_melt <- reshape2::melt(data, id.vars = "IdMain")
edgelist <- data_melt[!is.na(data_melt$value), c("IdMain", "value")]
但是,这只会创建一个定向"边列表(从Main到Partners).我需要的是类似下面的内容,其中记录了每次交互.
However, this only creates a 'directed' edgelist (from Main to Partners). What I need is something like below, where every interaction is recorded.
Id1 Id2
1 4
1 3
1 7
1 6
4 3
4 7
4 6
3 7
etc
有没有人给小费最好的方法是什么?我还查看了 igraph
库,找不到用于执行此操作的函数.
Does anyone have a tip what the best way to go is? I also looked into the igraph
library and couldn't find the function to do this.
推荐答案
不需要reshape(2)和熔化等.您只需要抓紧每对列对的组合,然后将它们绑定在一起即可.
There is no need for reshape(2) and melting etc. You just need to grap every combination of column pairs and then bind them together.
x <- read.table(text="IdMain IdPartner1 IdPartner2 IdPartner3 IdPartner4
1 4 3 7 6
2 3 1 NA NA
3 1 4 2 NA
4 9 6 3 NA", header=TRUE)
idx <- t(combn(seq_along(x), 2))
edgelist <- lapply(1:nrow(idx), function(i) x[, c(idx[i, 1], idx[i, 2])])
edgelist <- lapply(edgelist, setNames, c("ID1","ID2"))
edgelist <- do.call(rbind, edgelist)
edgelist <- edgelist[rowSums(is.na(edgelist))==0, ]
edgelist
# ID1 ID2
# 1 1 4
# 2 2 3
# 3 3 1
# 4 4 9
# 5 1 3
# 6 2 1
# 7 3 4
# 8 4 6
# 9 1 7
# 11 3 2
# 12 4 3
# 13 1 6
# 17 4 3
# 18 3 1
# 19 1 4
# 20 9 6
# 21 4 7
# 23 1 2
# 24 9 3
# 25 4 6
# 29 3 7 <--
# 31 4 2
# 32 6 3
# 33 3 6 <--
# 37 7 6 <--
这篇关于为data.frame中的所有交互创建边缘列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!