R,通过名称访问矩阵的列向量 [英] R, accessing a column vector of a matrix by name
本文介绍了R,通过名称访问矩阵的列向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在R中,我可以通过以下方式访问列矩阵的列向量中的数据:
In R I can access the data in a column vector of a column matrix by the following:
mat2[,1]
mat2
的每一列都有一个名称.如何使用name属性而不是[,1]
从第一列中检索数据?
Each column of mat2
has a name. How can I retrieve the data from the first column by using the name attribute instead of [,1]
?
例如,假设我的第一列的名称为"saturn".我想要类似的东西
For example suppose my first column had the name "saturn". I want something like
mat2[,1] == mat2[saturn]
推荐答案
请执行以下操作:
mat2[,'saturn']
例如:
> x <- matrix(1:21, nrow=7, ncol=3)
> colnames(x) <- paste('name', 1:3)
> x[,'name 1']
[1] 1 2 3 4 5 6 7
这篇关于R,通过名称访问矩阵的列向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文