Reordering columns in a large dataframe

r

Solution

If you're just moving certain columns to the end, you can create a little helper-function like the following:

movetolast <- function(data, move) {
  data[c(setdiff(names(data), move), move)]
}

movetolast(df, c("b", "c"))
#   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

I would not recommend getting too into the habit of using column positions, especially not from a programmatic standpoint, since those positions might change.

"For fun" update

Here's an extended interpretation of the above function. It allows you to move columns to either the first or last position, or to be before or after another column.

moveMe <- function(data, tomove, where = "last", ba = NULL) {
  temp <- setdiff(names(data), tomove)
  x <- switch(
    where,
    first = data[c(tomove, temp)],
    last = data[c(temp, tomove)],
    before = {
      if (is.null(ba)) stop("must specify ba column")
      if (length(ba) > 1) stop("ba must be a single character string")
      data[append(temp, values = tomove, after = (match(ba, temp)-1))]
    },
    after = {
      if (is.null(ba)) stop("must specify ba column")
      if (length(ba) > 1) stop("ba must be a single character string")
      data[append(temp, values = tomove, after = (match(ba, temp)))]
    })
  x
}

Try it with the following.

moveMe(df, c("b", "c"))
moveMe(df, c("b", "c"), "first")
moveMe(df, c("b", "c"), "before", "e")
moveMe(df, c("b", "c"), "after", "e")

You'll need to adapt it to have some error checking--for instance, if you try to move columns "b" and "c" to "before c", you'll (obviously) get an error.

Problem

Using the following example dataframe: ``` a <- c(1:5) b <- c("Cat", "Dog", "Rabbit", "Cat", "Dog") c <- c("Dog", "Rabbit", "Cat", "Dog", "Dog") d <- c("Rabbit", "Cat", "Dog", "Dog", "Rabbit") e <- c("Cat", "Dog", "Dog", "Rabbit", "Cat") f <- c("Cat", "Dog", "Dog", "Rabbit", "Cat") df <- data.frame(a,b,c,d,e,f) ``` I want to investigate how to reorder the columns WITHOUT having to type in all the column names, i.e., `df[,c("a","d","e","f","b","c")]` How would I just say I want columns b and c AFTER column f? (only referencing the columns or range of columns that I want to move?). Many thanks in advance for your help.

Original source