R,按名称访问矩阵的列向量 [英] R, accessing a column vector of a matrix by name

查看:10
本文介绍了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屋!

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