subsetting 1-column matrix deletes rownames [duplicate]

夙愿已清 提交于 2020-12-26 07:39:48

问题


When I try to subset a 1-colum matrix by it's row names the subsetting works but an numeric vector is returned.

can you somehow prevent that behaviour and keep the row names?

M<-as.matrix(rnorm(5))
rownames(M)<-LETTERS[1:5]
M
        [,1]
A  0.6250957
B  0.7330598
C -0.7127075
D  0.2162602
E  0.2223444

M <- M[which(rownames(M) != "A")]

M
## [1]  0.7330598 -0.7127075  0.2162602  0.2223444

回答1:


you can read about argument drop in the help page: ?'['

M[which(rownames(M) != "A"), ,drop=FALSE]


来源:https://stackoverflow.com/questions/28294590/subsetting-1-column-matrix-deletes-rownames

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!