I have this dataframe (with the same values for each column to facilitate the example)
df = data.frame(x1 = c(1,2,3,4,5),
x2 = c(1,2,3,4,5),
x3 = c(1,2,3,4,5),
x4 = c(1,2,3,4,5),
x5 = c(1,2,3,4,5),
x6 = c(1,2,3,4,5),
x7 = c(1,2,3,4,5),
x8 = c(1,2,3,4,5))
I want to put x7 and x8 at the place of x2 and x3 while keeping the names of x7 and x8 and deleting x2 and x3 at the same time (the dataframe I am working on is really big and I can't use the simple method of df[,c(1,7,8,4,5,6)]
) so I am looking for a function to make it easier to apply. thanks
CodePudding user response:
Would this work ?
library(dplyr)
df %>%
relocate(c(x7,x8), .after = c(x2, x3)) %>%
select(-c(x2, x3))
#> x1 x7 x8 x4 x5 x6
#> 1 1 1 1 1 1 1
#> 2 2 2 2 2 2 2
#> 3 3 3 3 3 3 3
#> 4 4 4 4 4 4 4
#> 5 5 5 5 5 5 5
CodePudding user response:
You could try this flexible base R approach:
reorder_fun <- function(move_cols, splitpoint, datframe){
a <- 1:grep(splitpoint, names(datframe))
b <- c(a, grep(paste(move_cols, collapse = "|"), names(datframe)))
newdf <- cbind(datframe[, b],
datframe[, setdiff(names(datframe), names(datframe[b]))])
newdf
}
reorder_fun(move_cols = c("x7", "x8"), splitpoint = "x1", datframe = df)
Output:
# x1 x7 x8 x2 x3 x4 x5 x6
# 1 1 1 1 1 1 1 1 1
# 2 2 2 2 2 2 2 2 2
# 3 3 3 3 3 3 3 3 3
# 4 4 4 4 4 4 4 4 4
# 5 5 5 5 5 5 5 5 5
This allows you to move the desired columns to other positions (i.e, after the 3rd column is:
reorder_fun(move_cols = c("x7", "x8"), splitpoint = "x3", datframe = df)
or if you wanted to move different columns
reorder_fun(move_cols = c("x5", "x8"), splitpoint = "x1", datframe = df)