Select the last n columns of the data frame in R

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) # Move A and B to the front of mydata2 mydata2_move = data.frame(A = mydata2$A, B = mydata2$B, mydata2[,1: (ncol(mydata2)-2)]) # AB num1 num2 #1 AB 1 36 #2 AB 2 37 #3 AB 3 38 #4 AB 4 39 #5 AB 5 40 

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 
+8
r indexing dataframe
source share
5 answers

You can use something like this:

 move_to_start <- function(x, to_move) { x[, c(to_move, setdiff(colnames(x), to_move))] } move_to_start(mydata2, c('A', 'B')) # AB num1 num2 # 1 AB 1 36 # 2 AB 2 37 # 3 AB 3 38 # 4 AB 4 39 # 5 AB 5 40 

Alternatively, if you want to move the last columns of n to the beginning:

 move_to_start <- function(x, n) { x[, c(tail(seq_len(ncol(x)), n), seq_len(ncol(x) - n))] } move_to_start(mydata2, 2) # AB num1 num2 # 1 AB 1 36 # 2 AB 2 37 # 3 AB 3 38 # 4 AB 4 39 # 5 AB 5 40 
+4
source share

The described problem does not correspond to the name, and the existing answers concern part of the moving columns, in fact they do not explain how to select the last N columns.

If you want to simply select the last n columns in the matrix / data frame without knowing the column names:

 mydata2[,ncol(mydata2)] 

and if you want to use the last n columns try

 mydata[,(ncol(mydata2)-n-1):ncol(mydata2)] 

A little cumbersome, but it works. You can write a wrapper function if you plan to use it regularly.

+4
source share

data frames are only lists, so you can change them like any list:

 newdata <- c(mydata[colNamesToStart], mydata[-which(names(mydata) %in% colNamesToStart)]) 
+2
source share

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] # AB num1 num2 #1 AB 1 36 #2 AB 2 37 #3 AB 3 38 #4 AB 4 39 #5 AB 5 40 

You can move column names, for example, in this example from the 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" 
+2
source share

I know this topic is a bit dead, but wanted to listen with a simple dplyr solution:

 library(dplyr) mydata <- mydata %>% select(A, B, everything()) 

I hope this helps any future visitors in this matter.

+1
source share

All Articles