如何使用矩阵显示R中的关系? [英] How to use matrix to show relationship in R?
本文介绍了如何使用矩阵显示R中的关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在这里有一个列表x:
I have a list x here:
我想展示这样的元素之间的关系:
I want to show the relationship between the elements like this:
有人可以告诉我如何在R中执行此操作吗? 非常感谢你!
Can anyone tell me how to do this in R? Thank you very much!
推荐答案
首先,从原始列表中构建所有对的矩阵:
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)
然后,使用此处显示的方法之一: R中的成对交互矩阵.我喜欢使用图论工具的一种:-)
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
这篇关于如何使用矩阵显示R中的关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文