Reorder Columns

Original source: http://stackoverflow.com/questions/5620885/how-does-one-reorder-columns-in-r

Want to learn more? I recommend working through: R for Data Science, R Cookbook, and R Graphics Cookbook.

# create some simulated data
df <- data.frame(1,2,3,4)
df
  X1 X2 X3 X4
1  1  2  3  4
# move the third element to the second element's spot
df <- df[,c(1,3,2,4)]
df
  X1 X3 X2 X4
1  1  3  2  4