如何从R中的矩阵创建边列表? [英] How to create an edge list from a matrix in R?

查看:135
本文介绍了如何从R中的矩阵创建边列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这种关系表示为矩阵x,如下所示:

The relationship is expressed as a matrix x like this:

      A    B    C     D
A     0    2    1     1
B     2    0    1     0
C     1    1    0     1
D     1    0    1     0

条目是指它们具有的连接数.

The entries refer to the number of connections they have.

有人可以告诉我如何将其写为边列表吗?

Could anyone show me how to write it as an edge list?

我希望将其写为边列表:

I would prefer to write it as an edge list:

A B
A B
A C
A D
B C

但是这个边缘列表可以让我创建网络图吗?

But would this edge list allow me to create a network plot?

推荐答案

使用igraph软件包:

x <- matrix(c(0,2,1,1,2,0,1,0,1,1,0,1,1,0,1,0), 4, 4)
rownames(x) <- colnames(x) <- LETTERS[1:4]

library(igraph)
g <- graph.adjacency(x)
get.edgelist(g)

#      [,1] [,2]
#  [1,] "A"  "B" 
#  [2,] "A"  "B" 
#  [3,] "A"  "C" 
#  [4,] "A"  "D" 
#  [5,] "B"  "A" 
#  [6,] "B"  "A" 
#  [7,] "B"  "C" 
#  [8,] "C"  "A" 
#  [9,] "C"  "B" 
# [10,] "C"  "D" 
# [11,] "D"  "A" 
# [12,] "D"  "C"

我还建议您花一些时间阅读 http://igraph.sourceforge上的igraph文档. .net/index.html ,因为您最近的很多问题都是简单的案例用法.

I would also recommend you spend some time reading the igraph documentation at http://igraph.sourceforge.net/index.html since a lot of your recent questions are all simple case usages.

(作为奖励,plot(g)将回答您的其他问题如何绘制关系在R中?)

(As a bonus, plot(g) will answer your other question How to plot relationships in R?)

这篇关于如何从R中的矩阵创建边列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆