Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

deleting and ordering columns in a big dataframe

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading