[R] Matrix dimnames

Michael Prager mike.prager at noaa.gov
Thu Nov 2 02:12:49 CET 2006


Daniel Gatti wrote:

> How does one assign names to the columns of a matrix so that the columns 
> can be accesses using the '$' operator? It seem that x$c1 below should 
> return column 1.
> 
>  > x = matrix(1:4,2)
>  > dimnames(x) = list(c("r1", "r2"), c("c1", "c2"))
>  > x
>     c1 c2
> r1  1  3
> r2  2  4
>  > x$c1
> NULL
>  > x$c2
> NULL
> 

Is this useful?--

> x[,"c1"]
r1 r2 
 1  2 

I have only seen "$" used with lists.  A data frame is a kind
of list, but a matrix isn't.  

>From the R Reference Manual: "The form [of indexing] using $
applies to recursive objects such as lists and pairlists."

HTH

Mike Prager
Southeast Fisheries Science Center, NOAA
Beaufort, North Carolina  USA



More information about the R-help mailing list