如何使用矩陣顯示R中的關系? - How to use matrix to show relationship in R? -开发者知识库

如何使用矩陣顯示R中的關系? - How to use matrix to show relationship in R? -开发者知识库,第1张

I have a list x here: 如何使用矩陣顯示R中的關系? - How to use matrix to show relationship in R? -开发者知识库,enter image description here,第2张

我在這里有一個清單x:

I want to show the relationship between the elements like this: 如何使用矩陣顯示R中的關系? - How to use matrix to show relationship in R? -开发者知识库,enter image description here,第3张

我想展示這樣的元素之間的關系:

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

誰能告訴我如何在R中做到這一點?非常感謝你!

1 个解决方案

#1


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 :-)

然后,使用此處顯示的方法之一:R中的成對交互矩陣。我喜歡使用圖論工具的方法:-)

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

最佳答案:

DABAN RP主题是一个优秀的主题,极致后台体验,无插件,集成会员系统
U19学习网站 » 如何使用矩陣顯示R中的關系? - How to use matrix to show relationship in R? -开发者知识库