Hello, 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? Thank you, Shengzhe
Prof Brian Ripley
2005-Jul-19 07:49 UTC
[R] help: how to change the column name of data.frame
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. 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
Prof Brian Ripley
2005-Jul-19 08:20 UTC
[R] help: how to change the column name of data.frame
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
Thanks a lot! But DF[c(4,6)] <- DF[c(6,4)] seems to just exchange the data, not the names. If exchanging the columns(both data and names) needs two steps? DF[c(4,6)] <- DF[c(6,4)] names(DF)[c(4,6)] <- names(DF)[c(6,4)] Shengzhe
Prof Brian Ripley
2005-Jul-19 10:49 UTC
[R] help: how to change the column name of data.frame
On Tue, 19 Jul 2005, wu sz wrote:> Thanks a lot! But DF[c(4,6)] <- DF[c(6,4)] seems to just exchange > the data, not the names. If exchanging the columns(both data and > names) needs two steps? > > DF[c(4,6)] <- DF[c(6,4)] > names(DF)[c(4,6)] <- names(DF)[c(6,4)]Yes, it does. You did however say in your first message that you wanted to exchange the data, despite the subject line. As I asked before *PLEASE* do try to be precise in what you want to do. -- 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