[R] colnames

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue Jul 19 16:31:27 CEST 2005


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 at 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




More information about the R-help mailing list