[R] dimnames

Bill Venables William.Venables at cmis.CSIRO.AU
Sun Nov 14 22:12:31 CET 1999


Christian Stratowa asks:

> Hi,
> 
> Maybe this is a simple question:
> 
> Using S-Plus, and having a matrix m with row- and col-names, I
> have the following problem:
> 
> When I import into a function the one-column matrix m[,i], I am
> no longer able to get the column name.

Yes.  This is a phenomenon of R and S that we might call a
"helpful inconsistency".  If you select a single row or column of
a matrix in this way the object you get is not a matrix at all
but a vector (which does not have rows or columns).  Often this
is what you want so the convention is helpful, but it is an
inconsistency that sometimes causes problems as it has with you.
(It has nothing to do with passing on the object to functions, by
the way: it always happens.)

> Is there a way to get the column-name?

You can select the column in a way that does give you a single
column matrix (complete with column name) by using m[,i,drop=F]
instead of m[,i].  

Odd as it may look, this is often what you need to do in
programming when you are relying on some selected set of columns
to be a matrix and not something simpler.

> (Although attributes(m) displays row- and col-names,
> attributes(m[,i]) does no longer show the col-name.)

That's because they have been "dropped".  If you turn off
dropping you will preserve the attribute.

I can't help feeling that this convention, helpful as it might be
in interactive work, is a bit of a mistake.  It second-guesses
what you mean rather than what you actually program.  In that
respect it is for me an uncomfortable reminder of the sort of
thing that goes on in much MicroSoft software...

Bill Venables.

-- 
-----------------------------------------------------------------
Bill Venables, Statistician, CMIS Environmetrics Project.

Physical address:                            Postal address:
CSIRO Marine Laboratories,                   PO Box 120,       
233 Middle St, Cleveland, Queensland         Cleveland, Qld, 4163
AUSTRALIA                                    AUSTRALIA

Telephone: +61 7 3826 7251     Email: Bill.Venables at cmis.csiro.au     
      Fax: +61 7 3826 7304
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list