简体   繁体   中英

matrix node with same attribute transformation to matrix in igraph

在此处输入图片说明I have a matrix in igraph, as the 1st in the above pic. The numbers mean cooperation times. I have a attibute, as the 2nd. I would like a code that can automatically give me the 3rd matrix in igraph or other in R. Thx in adavnce.

Non- igraph approach

You can try the code below (don't need igraph )

> +((with(a, outer(type, type, `==`)) * mat) > 0)
  A B C
A 0 0 1
B 0 0 0
C 1 0 0

igraph approach

If you want to use igraph anyway, one possible option might be

graph_from_adjacency_matrix(mat > 0, mode = "undirected") %>%
    get.data.frame() %>%
    filter(a[from, ] == a[to, ]) %>%
    graph_from_data_frame(directed = FALSE, vertices = row.names(a)) %>%
    get.adjacency(sparse = FALSE)

which gives

  A B C
A 0 0 1
B 0 0 0
C 1 0 0

Data

mat <- `dimnames<-`(
    matrix(c(0, 2, 3, 2, 0, 1, 3, 1, 0), 3),
    rep(list(LETTERS[1:3]), 2)
)

a <- `rownames<-`(data.frame(type = c("edu", "company", "edu")), LETTERS[1:3])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM