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

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue Jul 19 10:20:55 CEST 2005

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