R move column to last using dplyr

dplyrr

For a data.frame with n columns, I would like to be able to move a column from any of 1-(n-1) positions, to be the nth column (i.e. a non-last column to be the last column). I would also like to do it using dplyr. I would like to do so without simply typing out the names of all the columns.

For example:

data<-data.frame(a=1:5, b=6:10, c=11:15)

This works, but isn't the dplyr way:

data[,c(colnames(data)[colnames(data)!='b'],'b')]

This is the dplyr way to make column b first:

data%>%select(b, everything())

But this doesn't work to make column b last:

data%>%select(everything(), b)

This works, but requires me to type out all the columns:

data%>%select(a,c,b)

So is there an elegant dplyr way to do this?

Related questions:

Best Answer

After some tinkering, the following works and requires very little typing.

data %>% select(-b,b)


UPDATE: dplyr 1.0.0

dplyr 1.0.0 introduces the relocate verb:

data %>% relocate(b, .after = last_col())

I still prefer the old "hacky" way.