R-将邻接数据帧转换为边缘列表类型列表 [英] R- Convert adjacency data frame to edgelist type list

查看:130
本文介绍了R-将邻接数据帧转换为边缘列表类型列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将具有命名列和行(NxN维度)的数据框更改为边列表.

I need to change a data frame with named column and rows (NxN dimension) into an edge list.

例如,数据框中的矩阵为:

For example the matrix in the data frame is:

     C1  C2  C3  C4
Ane  0   1   31  0
Jol  4   14  2   0
Ruf  3   3   11  6 
Rei  1   7    0  0

我想将其更改为列表(如边列表):

I want to change this to a list (like an edge list):

Ane-C1  0
Ane-C2  1
Ane-C3  31
Ane-C4  0
Jol-C1  4
Jol-C2  14
Jol-C3  2
Jol-C4  0
Ruf-C1  3
Ruf-C2  3
Ruf-C3  11
Ruf-C4  6
Rei-C1  1
Rei-C2  7
Rei-C3  0
Rei-C4  0

我发现其他线程似乎只有列名/数据框不包含邻接矩阵.

The other threads that I found seems to have only column names/ the data frame is not containing an adjacency matrix.

我尝试将其绘制为图形,然后获取get.edgelist,还尝试了[order(df [,1],df [1,]),, drop = FALSE],但是这些没有给出我想要的. 有人可以教我吗?

I tried to make it a graph and then get.edgelist, also tried [order(df[,1],df[1,]),,drop=FALSE] but those doesn't give what I want. Can anyone help teach me?

推荐答案

base R中,复制行名和列名,paste对其进行复制,并根据其值和矩阵值创建data.frame

In base R, replicate the rownames and column names, paste it, create a data.frame based on that along with the values of the matrix

data.frame(name = paste(rownames(m1)[col(m1)], colnames(m1)[row(m1)], sep="-"),
          val = c(t(m1)), stringsAsFactors = FALSE)
#     name val
#1  Ane-C1   0
#2  Ane-C2   1
#3  Ane-C3  31
#4  Ane-C4   0
#5  Jol-C1   4
#6  Jol-C2  14
#7  Jol-C3   2
#8  Jol-C4   0
#9  Ruf-C1   3
#10 Ruf-C2   3
#11 Ruf-C3  11
#12 Ruf-C4   6
#13 Rei-C1   1
#14 Rei-C2   7
#15 Rei-C3   0
#16 Rei-C4   0

这篇关于R-将邻接数据帧转换为边缘列表类型列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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