子集 1 列矩阵删除行名 [英] subsetting 1-column matrix deletes rownames

查看:19
本文介绍了子集 1 列矩阵删除行名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试通过行名称对 1 列矩阵进行子集化时,子集化工作但返回一个数字向量.

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

推荐答案

你可以在帮助页面中阅读关于参数 drop 的内容:?'['

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

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

这篇关于子集 1 列矩阵删除行名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆