How to use matrix to show relationship in R?

心不动则不痛 提交于 2019-12-11 14:45:44

问题


I have a list x here:

I want to show the relationship between the elements like this:

Can anyone tell me how to do this in R? Thank you very much!


回答1:


First, build a matrix of all pairs from your original list:

L <- list(c("John", "Mary", "Jack"), c("John", "Wendy"), c("Mary", "Wendy"))
x <- matrix(unlist(lapply(L, combn, 2, simplify = FALSE)), ncol = 2)

Then, use one of the methods shown here: Pairwise interaction matrix in R. I like the one using graph theory tools :-)

library(igraph)
g <- graph.edgelist(x, directed = FALSE)
get.adjacency(g)

#       John Jack Mary Wendy
# John     0    1    1     1
# Jack     1    0    1     0
# Mary     1    1    0     1
# Wendy    1    0    1     0


来源:https://stackoverflow.com/questions/13190627/how-to-use-matrix-to-show-relationship-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!