Extract matrix column values by matrix column name

SFun28 picture SFun28 · Apr 21, 2011 · Viewed 129.2k times · Source

Is it possible to get a matrix column by name from a matrix?

I tried various approaches such as myMatrix["test", ] but nothing seems to work.

Answer

Joris Meys picture Joris Meys · Apr 21, 2011

Yes. But place your "test" after the comma if you want the column...

> A <- matrix(sample(1:12,12,T),ncol=4)

> rownames(A) <- letters[1:3]

> colnames(A) <- letters[11:14]
> A[,"l"]
 a  b  c 
 6 10  1 

see also help(Extract)