R 将矩阵转换为长数据格式

R 将矩阵转换为长数据格式,r,networking,matrix,R,Networking,Matrix,我想将矩阵转换为一个较长的data.frame边列表,该列表由行到列组成,如下所示: set.seed(666) mat <- matrix(sample(0:1,12,replace=TRUE),nrow=3) library(bipartite) as.data.frame(web2edges(mat, return = TRUE)) 数据帧错误(行=c(“1”、“3”、“3”、“1”、“2”),列=c(“4”、“4”): 重复的行名称:g,e 是否有另一种不使用we

我想将矩阵转换为一个较长的data.frame边列表,该列表由行到列组成,如下所示:

  set.seed(666)
  mat <- matrix(sample(0:1,12,replace=TRUE),nrow=3)
  library(bipartite)
  as.data.frame(web2edges(mat, return = TRUE))
数据帧错误(行=c(“1”、“3”、“3”、“1”、“2”),列=c(“4”、“4”): 重复的行名称:g,e


是否有另一种不使用
web2edges
的简单方法可以实现这一点?

与矩阵不同,数据帧不能有重复的行名。因此,您必须在
web2edges
之前更改行名和列名,而不是在之后更改

set.seed(666)
mat <- matrix(sample(0:1,12,replace=TRUE),nrow=3)
library(bipartite)

#data frame with numerical values in `row` and `col` columns:
ndf <- as.data.frame(web2edges(mat, return = TRUE))

#converting `row` and `col` columns to factors:
ndf$col <- factor(ndf$col, levels = 4:7, labels = letters[1:4])
ndf$row <- factor(ndf$row, levels = 1:3, labels = letters[5:7])

> ndf
  row col edge.weights
1   e   a            1
2   g   a            1
3   g   b            1
4   e   c            1
5   f   d            1
set.seed(666)

mat可能会将
web2edges()
结果的行名设置为NULL,然后转换为data.frame?或者直接将
as.data.frame.table(mat)
设置为subset。我喜欢table and subset选项,@thelatemail如果您想回答,我很乐意接受。可能会重复
set.seed(666)
mat <- matrix(sample(0:1,12,replace=TRUE),nrow=3)
library(bipartite)

#data frame with numerical values in `row` and `col` columns:
ndf <- as.data.frame(web2edges(mat, return = TRUE))

#converting `row` and `col` columns to factors:
ndf$col <- factor(ndf$col, levels = 4:7, labels = letters[1:4])
ndf$row <- factor(ndf$row, levels = 1:3, labels = letters[5:7])

> ndf
  row col edge.weights
1   e   a            1
2   g   a            1
3   g   b            1
4   e   c            1
5   f   d            1