Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
To reorder columns by their numeric position:
dat<-dat[c(1,3,2)]
To reorder by column name:
dat<-dat[c("col1","col3","col2")]
The previous examples use list-style indexing. A data frame is essentially a list of vectors, and indexing into it as a list will return another data frame. You can get the same effect with matrix-style indexing:
library(gcookbook)# For the data setanthomingangle expt ctrlanthoming-2010-107302310032001[c(1,3,2)]# List-style indexingangle ctrl expt-2001-103703210302010# Putting nothing before the comma means to select all rowsanthoming[,c(1,3,2)]# Matrix-style indexingangle ctrl expt-2001-103703210302010