基于节点变量的igraph中的edgelist表的Matrix和data.frame

时间:2015-06-16 11:03:22

标签: r matrix graph igraph

我有一个igraph对象g如下。

g <- structure(list(48, FALSE,
                    c(7, 14, 27, 14, 27, 27, 12, 18, 18, 13, 16, 20, 41, 20, 41, 41,
                      17, 26, 26, 19, 22, 45, 45, 24, 34, 34, 25, 29, 42, 42, 30, 33,
                      39, 33, 39, 39, 31, 35, 46, 35, 46, 46, 37, 40, 44, 47),
                    c(0, 0, 0, 7, 7, 14, 6, 6, 12, 10, 2, 2, 2, 16, 16, 20, 15, 15,
                      17, 11, 21, 21, 22, 23, 23, 24, 9, 3, 3, 29, 5, 5, 5, 30, 30,
                      33, 4, 4, 4, 31, 31, 35, 36, 8, 1, 38),
                    c(0, 6, 9, 1, 3, 10, 16, 7, 8, 19, 11, 13, 20, 23, 26, 17, 18, 2,
                      4, 5, 27, 30, 36, 31, 33, 24, 25, 37, 39, 42, 32, 34, 35, 43,
                      12, 14, 15, 28, 29, 44, 21, 22, 38, 40, 41, 45),
                    c(0, 1, 2, 44, 10, 11, 12, 27, 28, 36, 37, 38, 30, 31, 32, 6, 7,
                      3, 4, 43, 26, 9, 19, 8, 5, 16, 17, 13, 14, 18, 15, 20, 21, 22,
                      23, 24, 25, 29, 33, 34, 39, 40, 35, 41, 42, 45),
                    c(0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 3, 5, 5, 6, 7, 9, 10,
                      12, 12, 13, 13, 14, 15, 17, 20, 20, 21, 22, 23, 23, 25, 27, 29,
                      29, 30, 30, 33, 34, 37, 39, 39, 40, 42, 45, 46),
                    c(0, 3, 4, 7, 9, 12, 15, 17, 19, 20, 21, 22, 23, 24, 24, 25, 27,
                      29, 30, 30, 30, 31, 33, 34, 36, 37, 37, 37, 37, 37, 38, 40, 42,
                      42, 43, 43, 44, 45, 45, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46),
                    list(c(1, 0, 1), structure(list(), .Names = character(0)), 
                         structure(list(name = c("KP1009", "GP3040", "KP1757","GP2243", "KP682", "KP1789", "KP1933", "KP1662", "KP1718",
                                                 "GP3339", "GP4007", "GP3398", "GP6720", "KP808", "KP1154", "KP748", "GP4263", "GP1132",
                                                 "GP5881", "GP6291", "KP1004", "KP1998", "GP4123", "GP5930", "KP1070", "KP905", "KP579",
                                                 "KP1100", "KP587", "GP913", "GP4864", "KP1513", "GP5979", "KP730", "KP1412", "KP615",
                                                 "KP1315", "KP993", "GP1521", "KP1034", "KP651", "GP2876", "GP4715", "GP5056", "GP555",
                                                 "GP408", "GP4217", "GP641"),
                                        Type = c("B", "A", "B", "A", "B", "B", "B", "B", "B", "A", "A", "A", "A", "B", "B", "B", "A",
                                                 "A", "A", "A", "B", "B", "A", "A", "B", "B", "B", "B", "B", "A", "A", "B", "A", "B",
                                                 "B", "B", "B", "B", "A", "B", "B", "A", "A", "A", "A", "A", "A", "A"),
                                        Set = c(15L, 1L, 10L, 21L, 5L, 9L, 12L, 15L, 16L, 19L, 22L, 3L, 12L, 22L, 15L, 25L, 10L, 25L,
                                                12L, 3L, 10L, 8L, 8L, 20L, 20L, 19L, 25L, 15L, 6L, 21L, 9L, 5L, 24L, 9L, 20L, 5L, 2L, 2L,
                                                11L, 9L, 16L, 10L, 21L, 4L, 1L, 8L, 5L, 11L),
                                        Loc = c(3L, 2L, 3L, 1L, 3L, 3L, 3L, 1L, 2L, 1L, 3L, 1L, 1L, 2L, 2L, 1L, 3L, 2L, 2L, 2L, 3L, 2L,
                                                3L, 2L, 1L, 3L, 3L, 3L, 2L, 3L, 1L, 3L, 3L, 1L, 3L, 2L, 3L, 1L, 1L, 1L, 2L, 3L, 3L, 3L,
                                                2L, 2L, 3L, 3L),
                                        color = c("lightskyblue", "wheat", "lightskyblue", "salmon", "lightskyblue", "lightskyblue",
                                                  "lightskyblue", "salmon", "wheat", "salmon", "lightskyblue", "salmon", "salmon",
                                                  "wheat", "wheat", "salmon", "lightskyblue", "wheat", "wheat", "wheat", "lightskyblue",
                                                  "wheat", "lightskyblue", "wheat", "salmon", "lightskyblue", "lightskyblue", "lightskyblue",
                                                  "wheat", "lightskyblue", "salmon", "lightskyblue", "lightskyblue", "salmon", "lightskyblue",
                                                  "wheat", "lightskyblue", "salmon", "salmon", "salmon", "wheat", "lightskyblue",
                                                  "lightskyblue", "lightskyblue", "wheat", "wheat", "lightskyblue", "lightskyblue")),
                                   .Names = c("name", "Type", "Set", "Loc", "color")), list())), class = "igraph")

如何获得各种关系中唯一成员节点的摘要?

基本上对于levels(as.factor(V(g)$Loc))中的每个级别,我想得到否。每种关系中涉及的节点如下。

代表 1

1-1

1-2

1-3

2

2-2

2-1

2-3

3

3-3

3-1

3-2

以下根据边缘类型给出了边的摘要。

DT <- data.frame(V(g)$name, V(g)$Loc)

edgelist <- merge(edgelist, DT, by.x = "V1",  by.y = "V.g..name", all.x = T)

colnames(DT) <- c("V.g..name", "V.g..Loc2")


edgelist <- merge(edgelist, DT, by.x = "V2",  by.y = "V.g..name", all.x = T)

out1 <- table(edgelist$V.g..Loc,edgelist$V.g..Loc2)
out2 <- as.data.frame(out1)
out1 <- as.data.frame.matrix(out1)

所需的输出是一个像out1这样的矩阵,其中没有。每种关系中涉及的独特节点,而不是关系类型。

1 个答案:

答案 0 :(得分:1)

首先,代码中的edgelist变量未指定,因此会引发错误。

如果我理解正确,您将需要使用以下内容:

E(g)[which(V(g)$Loc == 1) %--% which(V(g)$Loc == 1)]

您将逐步执行which语句中的值。