微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

R错误,无效的'nrow'值<0:将非方阵更改为方阵

如何解决R错误,无效的'nrow'值<0:将非方阵更改为方阵

使用R中的'networkd3'和'igraph'库,我试图在R中创建一个和弦网络,如下所示:https://www.rdocumentation.org/packages/networkD3/versions/0.4/topics/chordNetwork

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

以下是我的代码

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

有人知道我在做什么错吗?还是用这种图创建和弦网络是不可能的。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。