[R] How to order some of my columns (not rows) alphabetically

Mark Na mtb954 at gmail.com
Fri Sep 26 00:54:31 CEST 2008


Hello,

I have a dataframe with 9 columns, and I would like to sort (order) the 
right-most eight of them alphabetiaclly, i.e.:

ID1 ID2 F G A B C E D

would become

ID1 ID2 A B C D E F G

Right now, I'm using this code:

attach(data)
data<-data.frame(ID1,ID2,data[,sort(colnames(data)[3:9])])
detach(data)

but that's not very elegant. Ideally I could specify which columns to 
sort and which to leave "as is" (but my attempts to do so have failed).

Thank you,

Mark



More information about the R-help mailing list