获取包含 alter 的 alters 的边缘列表

Get edge list that includes alter's alters

我需要一个包含三列的数据框:i、j(改变)和 k(j 的改变)。我有一个邻接矩阵(下面的示例)。从那里我可以获得一个图形对象并提取边缘列表。我如何操作数据以获得如下 WANT 数据框的输出?

有(矩阵和边缘列表):

      1   2   3   4   5   

 1    0   0   0   1   0  
 2    0   0   1   1   1   
 3    0   0   0   0   0   
 4    1   1   0   0   1   
 5    1   1   0   1   0    


g <- graph_from_adjacency_matrix(mat)

get.edgelist(g)


i   j

1   4
2   3
2   4
2   5
4   1
4   2
4   5
5   1
5   2
5   4

想要(ijk 边列表):

i j k
1 4 2
1 4 5
2 4 1
2 4 5
4 2 3
4 5 1
4 5 2
5 1 4 
5 2 3
5 2 4
5 4 1
5 4 2

ijk 边列表应该包含 ij 的所有可能的三元组,不包括自循环(例如:1 4 1)

我实际上能够使用 igraph 和 dplyr 找到一种方法:

# make graph of matrix
g <- graph_from_adjacency_matrix(mat)


# put edgelist into two objects, one where columns are "i, j" and the other "j, k"
df1 <- get.edgelist(g) %>%
       as.data.frame() %>%
       select(i = V1, j = V2)

df2 <- get.edgelist(g) %>%
       as.data.frame() %>%
       select(j = V1, k = V2)

# combine the dataframes, filter out rows where i and k are the same observation
df_combn <- inner_join(df1, df2, by = c("j" = "j")) %>%
            mutate_all(as.character) %>%
            filter(., !(i == k))

数据:

as.matrix(read.table(text = "0   0   0   1   0  
                             0   0   1   1   1   
                             0   0   0   0   0   
                             1   1   0   0   1   
                             1   1   0   1   0",
                     header = F, stringsAsFactors = F)) -> m1

dimnames(m1) <- list(1:5, 1:5)

图书馆:

library(igraph) 
library(dplyr)
library(tidyr)
library(magrittr)

解法:

g1 <- graph_from_adjacency_matrix(m1)
e1 <- get.edgelist(g1) %>% as.data.frame %>% mutate_if(is.factor, as.character)

e1 %>% 
  group_by(V1) %>% 
  nest(V2) %>% 
  right_join(e1,.,by = c("V2"="V1")) %>%  
  unnest %>% 
  filter(V1 != V21) %>% 
  set_colnames(c("i", "j", "k"))

输出:

#>    i j k
#> 1  1 4 2
#> 2  1 4 5
#> 3  2 4 1
#> 4  2 4 5
#> 5  2 5 1
#> 6  2 5 4
#> 7  4 2 3
#> 8  4 2 5
#> 9  4 5 1
#> 10 4 5 2
#> 11 5 1 4
#> 12 5 2 3
#> 13 5 2 4
#> 14 5 4 1
#> 15 5 4 2