我有下表
1 "x1" "x2"
1 "x1" "x2" "x3"
1 "x1" "x2"
2 "y1" "y2" "y3"
2 "y1" "y2" "y3"
3 "y1" "x2"
3 "z1" "x2"
我需要将此表转换为邻接矩阵或边缘列表,其中我可以将第一列作为边的属性,其余列将是我的边,例如,我需要的行有超过3个边缘,它们都像这样连接(对于第2行):
"x1" "x2" 1
"x1" "x3" 1
"x2" "x3" 1
一个意思是我想要的边缘类型。
有没有办法在R或python中执行此操作?
我要用R中的igraph来绘制这个。
答案 0 :(得分:2)
我找不到简单的转换。但是使用您的样本数据:
dd <- structure(list(V1 = c(1L, 1L, 1L, 2L, 2L, 3L, 3L), V2 = structure(c(1L,
1L, 1L, 2L, 2L, 2L, 3L), .Label = c("x1", "y1", "z1"), class = "factor"),
V3 = structure(c(1L, 1L, 1L, 2L, 2L, 1L, 1L), .Label = c("x2",
"y2"), class = "factor"), V4 = structure(c(1L, 2L, 1L, 3L,
3L, 1L, 1L), .Label = c("", "x3", "y3"), class = "factor")), .Names = c("V1",
"V2", "V3", "V4"), class = "data.frame", row.names = c(NA, -7L))
我最终创建了一些辅助函数,并使用magrittr
喜欢使用的dplyr
语法来实现此
library(magrittr)
smoosh <- function(...) do.call(Map, c(list(cbind.data.frame), list(...)))
collpase <- function(x) do.call(rbind, x)
has.char <- function(x) x[nchar(x)>0]
xx <- dd[-1] %>% as.matrix %>% split( 1:nrow(dd)) %>% lapply(has.char) %>%
lapply(combn,2) %>% lapply(t) %>% smoosh(attr=dd$V1) %>% collpase
正如您所看到的那样,有一些小的转换,这就是为什么我选择将%>%
运算符用于magrittr
而不是将它们全部嵌套以便于阅读。但最终它会返回
1 2 attr
1 x1 x2 1
2.1 x1 x2 1
2.2 x1 x3 1
2.3 x2 x3 1
3 x1 x2 1
4.1 y1 y2 2
4.2 y1 y3 2
4.3 y2 y3 2
5.1 y1 y2 2
5.2 y1 y3 2
5.3 y2 y3 2
6 y1 x2 3
7 z1 x2 3