Re: [R] help: how to change the column name of data.frame

From: Prof Brian Ripley <ripley_at_stats.ox.ac.uk>
Date: Tue 19 Jul 2005 - 18:20:55 EST

On Tue, 19 Jul 2005, Prof Brian Ripley wrote:

> Just change the names! E.g.
>
> names(DF)[c(4,6)] <- names(DF)[c(6,4)]
>
> Strictly a data frame has names, not column names, hence the use the
> names() and names<-() functions here.

That answered the subject line. If you want to exchange the columns (not just the data but also the names) you can just use

DF[c(4,6)] <- DF[c(6,4)]

Please do try to be more precise as to what you want to do, for example by giving an example.

> On Tue, 19 Jul 2005, wu sz wrote:
>
>> I have a data frame with 15 variables, and want to exchange the data
>> of 4th column and 6th column. First I append a column in the data
>> frame, copy the 4th column data there, then copy the 6th column data
>> to 4th column, and copy the appended column data to 6th column, but
>> the names of the 4th and 6th column are still unchanged. How can I
>> exchange them?

-- 
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 Tue Jul 19 18:24:48 2005

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