[R] Change positions of columns in data frame

Tom Gottfried tom.gottfried at wzw.tum.de
Fri Oct 23 18:28:43 CEST 2009


You can change the order of nearly everything just by applying an appropriate index and assigning to
the original object. Something like

data <- data[,order_of_columns]

Tom

Joel Fürstenberg-Hägg schrieb:
> Hi all,
> 
> Probably a simple question, but I just can't find a simple answear in the older threads or anywhere else.
> 
> I've added some new vectors as columns in a data frame using cbind(). As they're all put as the last columns inte the data frame, I would like to move them to specific positions. How do you do to change the position of a column in a data frame?
> 
> I know I can use fieldTrial0809=data.frame(Sample_ID=as.factor(fieldTrial0809$Sample_ID), Plant_ID=as.factor(fieldTrial0809$Plant_ID), ...) to create a new data frame with the given columns in the specified order, but there must be an easier way..?
> 
> All the best,
> 
> Joel
>  		 	   		  
> _________________________________________________________________
> Nya Windows 7 - Hitta en dator som passar dig! Mer information. 
> http://windows.microsoft.com/shop
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

-- 
Technische Universität München
Department für Pflanzenwissenschaften
Lehrstuhl für Grünlandlehre
Am Hochanger 1
85350 Freising / Germany
Phone: ++49 (0)8161 715324
Fax:   ++49 (0)8161 713243
email: tom.gottfried at wzw.tum.de
http://www.wzw.tum.de/gruenland




More information about the R-help mailing list