[R] changing colnames in dataframes

Prof Brian Ripley ripley at stats.ox.ac.uk
Wed Dec 3 21:41:23 CET 2008


See the help page.  You name the arguments, e.g.

JJ <- data.frame( gender= c( as.character(rep( gender,3))) ,
                   J.value -c( F76 ,6-F83, F90) )

The help says

Arguments:

      ...: these arguments are of either the form 'value' or 'tag =
           value'.  Component names are created based on the tag (if
           present) or the deparsed argument itself.


On Wed, 3 Dec 2008, Wolfgang Lindner wrote:

> dear all,
>
> I'm building new dataframes from bigger one's using e.g. columns F76, F83,
> F90:
>
> JJ<-data.frame( c( as.character(rep( gender,3))) , c( F76,6- F83, F90) )
>
> Looking into JJ one has:
>
>    c.as.character.rep.gender..8...
> c.6...F73..F78..F79..F82..6...F84..F94..F106..F109
> 1                                     w                   2
> 2                                     w                   3
> 3                                     m                  1
> etc.
>
> Instead of the automatic colnames 'c.as.character.rep.gender..8' I like to
> have the colnames 'gender' and 'J.value'.
> I tried levels, labels etc but without success.
>
> OK, I can fix(FF) manually, but this is bad, because it has to be done again
> and again ..
> OK, I can do
>
> gender.J<-c( as.character(rep( gender,3)))
> Jvalue<-c( F76,6- F83, F90)
> JJ<-data.frame(gender.J, J.value)
>
> but this is not optimal because 'gender' is depending on rep(.., n) and I
> have many dataframes KK, LL, .. using 'gender' with different size.
>
> Question: is it possible to do something like
>
> JJ<-data.frame(c( as.character(rep( gender,3))),c( F76,6- F83, F90),  ??
> col.names=c("gender","Jvalue") ?? )
>
> Sorry, if my question is elementary, but I could not find a fix.
>
> Thank your for your advice.
>
> hth
>
> Wolfgang

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