R, accessing a column vector of a matrix by name

attributes, matrix, r

Solution

The following should do it:

mat2[,'saturn']

For example:

> 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

Problem

In R I can access the data in a column vector of a column matrix by the following: ``` mat2[,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]`? For example suppose my first column had the name "saturn". I want something like `mat2[,1] == mat2[saturn]`

Original source

Related problems