I have created a matrix in R and. 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.
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
rownames(cor) <- cols[-1]
or if cols is longer than 160, you can do
rownames(cor) <- cols[2:160]
since you need exactly 159 names for each of the 159 rows in your matrix.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.