How does one reorder columns in a data frame?
Your dataframe has four columns like so df[,c(1,2,3,4)]
.Note the first comma means keep all the rows, and the 1,2,3,4 refers to the columns.
To change the order as in the above question do df2[,c(1,3,2,4)]
If you want to output this file as a csv, do write.csv(df2, file="somedf.csv")
You can also use the subset function:
data <- subset(data, select=c(3,2,1))
You should better use the [] operator as in the other answers, but it may be useful to know that you can do a subset and a column reorder operation in a single command.
Update:
You can also use the select function from the dplyr package:
data = data %>% select(Time, out, In, Files)
I am not sure about the efficiency, but thanks to dplyr's syntax this solution should be more flexible, specially if you have a lot of columns. For example, the following will reorder the columns of the mtcars dataset in the opposite order:
mtcars %>% select(carb:mpg)
And the following will reorder only some columns, and discard others:
mtcars %>% select(mpg:disp, hp, wt, gear:qsec, starts_with('carb'))
Read more about dplyr's select syntax.