Using the following example dataframe:
a <- c(1:5)
b <- c(\"Cat\", \"Dog\", \"Rabbit\", \"Cat\", \"Dog\")
c <- c(\"Dog\", \"Rabbit\", \"Cat\", \"Do
The package dplyr
and the function dplyr::relocate
, a new verb introduced in dplyr 1.0.0
, does exactly what you are looking for.
df %>% dplyr::relocate(b, c, .after = f)
Use the subset function:
> df <- data.frame(a,b,c,d,e,f)
> df <- subset(df, select = c(a, d:f, b:c))
> df
a d e f b c
1 1 Rabbit Cat Cat Cat Dog
2 2 Cat Dog Dog Dog Rabbit
3 3 Dog Dog Dog Rabbit Cat
4 4 Dog Rabbit Rabbit Cat Dog
5 5 Rabbit Cat Cat Dog Dog