Is there a way to systematically select the last columns of a data frame? I would like to be able to move the last columns to be the first columns, but maintain the order of
You can do a similar thing using the SOfun package, available on GitHub.
library(SOfun)
foo <- moveMe(colnames(mydata2), "A, B before num1")
mydata2[, foo]
# A B num1 num2
#1 A B 1 36
#2 A B 2 37
#3 A B 3 38
#4 A B 4 39
#5 A B 5 40
You can move column names like this example from R Help.
x <- names(mtcars)
x
#[1] "mpg" "cyl" "disp" "hp" "drat" "wt" "qsec" "vs" "am" "gear" "carb"
moveMe(x, "hp first; cyl after drat; vs, am, gear before mpg; wt last")
#[1] "hp" "vs" "am" "gear" "mpg" "disp" "drat" "cyl" "qsec" "carb" "wt"