The three top-rated answers have a weakness.
If your dataframe looks like this
df <- data.frame(Time=c(1,2), In=c(2,3), Out=c(3,4), Files=c(4,5))
> df
Time In Out Files
1 1 2 3 4
2 2 3 4 5
then it's a poor solution to use
> df2[,c(1,3,2,4)]
It does the job, but you have just introduced a dependence on the order of the columns in your input.
This style of brittle programming is to be avoided.
The explicit naming of the columns is a better solution
data[,c("Time", "Out", "In", "Files")]
Plus, if you intend to reuse your code in a more general setting, you can simply
out.column.name <- "Out"
in.column.name <- "In"
data[,c("Time", out.column.name, in.column.name, "Files")]
which is also quite nice because it fully isolates literals. By contrast, if you use dplyr's select
data <- data %>% select(Time, out, In, Files)
then you'd be setting up those who will read your code later, yourself included, for a bit of a deception. The column names are being used as literals without appearing in the code as such.
help(Extract)
also known as?'['
– Joris Meys