问题
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