简体   繁体   中英

Restore matrix row and column names to defaults in R (e.g., [1,], [2,]…)

Can matrix row and column names be set to defaults (eg, [1,], [2,]... [,1], [,2]...) in R?

For example, is there a quick way to transform a matrix like this

x1 <- matrix(1:9,nrow=3,ncol=3,dimnames=list(1:3,letters[1:3]))

> x1
  a b c
1 1 4 7
2 2 5 8
3 3 6 9

into this

> x1
     [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9

You're looking for dimnames<- :

dimnames(x1) <- NULL

     [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9

You can see the help file by typing ?dimnames . It is also linked from ?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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM