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 as the first columns, but maintain the order of the columns when moving them. I need a way to do this that does not list all columns using a subset (data, select = c (all columns listed in new order)), because I will use a lot of different data frames.
Here is an example where I would like to move the last 2 columns to the front of the data frame. It works, but it is ugly.
A = rep("A", 5) B = rep("B", 5) num1 = c(1:5) num2 = c(36:40) mydata2 = data.frame(num1, num2, A, B)
Changing the number of columns in the original data frame causes problems. This works (see below), but the naming is discarded. Why do these two examples behave differently? Is there a better way to do this and generalize it?
mydata1_move = data.frame(A = mydata1$A, B = mydata1$B, mydata1[,1: (ncol(mydata1)-2)]) # AB mydata1...1..ncol.mydata1....2.. #1 AB 1 #2 AB 2 #3 AB 3 #4 AB 4 #5 AB 5
r indexing dataframe
Nancy
source share