R错误,无效';nrow';值(<;0):将非方矩阵更改为方矩阵

R错误,无效';nrow';值(<;0):将非方矩阵更改为方矩阵,r,matrix,igraph,networkd3,chord-diagram,R,Matrix,Igraph,Networkd3,Chord Diagram,使用R中的“networkd3”和“igraph”库,我正在尝试在R中创建一个和弦网络,如下所示: 我模拟了一些假数据,并将这些数据转换成邻接矩阵/图,然后创建弦网络。但是,将原始数据转换为所需格式似乎存在问题 下面是我的代码: library(igraph) library(networkD3) #create file from which to sample from x5 <- sample(1:100, 1100, replace=T) #convert to data fra

使用R中的“networkd3”和“igraph”库,我正在尝试在R中创建一个和弦网络,如下所示:

我模拟了一些假数据,并将这些数据转换成邻接矩阵/图,然后创建弦网络。但是,将原始数据转换为所需格式似乎存在问题

下面是我的代码:

library(igraph)
library(networkD3)

#create file from which to sample from
x5 <- sample(1:100, 1100, replace=T)
#convert to data frame
x5 = as.data.frame(x5)

#create first file (take a random sample from the created file)
a = sample_n(x5, 1000)
#create second file (take a random sample from the created file)
b = sample_n(x5, 1000)


#create dataframe
c = data.frame(a,b)
#rename column names
colnames(c) <- c("a","b")
c = as.matrix(c)

# change dimensions of the matrix, from: https://stackoverflow.com/questions/26458182/converting-to-rectangular-matrix-to-a-square-matrix-in-r 
d <- dim(c)
cn <- colnames(c)
#here is the error
reformat = rbind(c, matrix(0, diff(d), ncol(c), dimnames = list(cn[(d[1]+1):d[2]])))

#Error in matrix(0, diff(d), ncol(c), dimnames = list(cn[(d[1] + 1):d[2]])) : 
  invalid 'nrow' value (< 0)

#create graph
g1 <- graph_from_adjacency_matrix(reformat)

#create chord network (with the values from "a" and "b" appearing around the circle)
chordNetwork(Data = g1, 
             width = 500, 
             height = 500, labels = colnames(g1)
            )
库(igraph)
图书馆(网络3)
#创建要从中采样的文件
x5