Re: [R] colnames

From: Prof Brian Ripley <ripley_at_stats.ox.ac.uk>
Date: Wed 20 Jul 2005 - 00:31:27 EST

On Tue, 19 Jul 2005, Adaikalavan Ramasamy wrote:

> First, your problem could be boiled down to the following example. See
> how the colnames of the two outputs vary.
>
> df <- cbind.data.frame( "100"=1:2, "200"=3:4 )
> df/df
> X100 X200
> 1 1 1
> 2 1 1

That one is probably unintentional.

> m <- as.matrix( df ) # coerce to matrix class
> m/m
> 100 200
> 1 1 1
> 2 1 1
>
> It appears that whenever R has to create a new dataframe automatically,
> it tries to get nice colnames. See help(data.frame). I am not exactly
> sure why this behaviour is different when creating a matrix. But I do
> not think this is a major problem for most people. If you coerce your
> input to matrix, the problem goes away.

A data frame is column-oriented, and can be used as a source of variables, e.g. by attach() and the data= argument of all the model-fitting functions. That is not the purpose of matrices, but is why data.frames are made to have syntactic names for columns by default.

-- 
Brian D. Ripley,                  ripley@stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Received on Wed Jul 20 00:39:45 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:33:48 EST