[英]Error while naming rows and columns of a matrix in R
I have created a matrix in R and. 我在R和中创建了一个矩阵。 Now I want to name its rows and columns.
现在,我要命名其行和列。 I have a vector of names and want to assign those names to the rows and columns of my matrix.
我有一个名称向量,希望将这些名称分配给矩阵的行和列。 but it gives me this error: length of 'dimnames' [1] not equal to array extent Here is my code, col is a vector of names.
但是它给了我这个错误:'dimnames'[1]的长度不等于数组范围这是我的代码,col是名称的向量。
cor<-matrix( ,nrow=159,ncol=159)
index<-2
for(i in 1:nrow(cor)){
rownames(cor)[i]<-cols[index]
index<-index+1
}
Assuming that cols
is a character vector of length 160, then you don't need a loop, you can just do 假设
cols
是长度为160的字符向量,那么您不需要循环,您可以
rownames(cor) <- cols[-1]
or if cols is longer than 160, you can do 或者如果cols大于160,则可以
rownames(cor) <- cols[2:160]
since you need exactly 159 names for each of the 159 rows in your matrix. 因为矩阵中的159行中的每一行都需要159个名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.