Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moving columns within a data.frame() without retyping

Tags:

r

Is there a method for moving a column from one position in a data.frame to the next - without typing an entirely new data.frame()

For example:

a <- b <- c <- d <- e <- f <- g <- 1:100 df <- data.frame(a,b,c,d,e,f,g) 

Now let's say I wanted "g" in front of "a"

I could retype it, as

df <- data.frame(g,a,b,c,d,e,f) 

But is there not a quicker way? (Imagine 1500+ columns)

like image 925
Brandon Bertelsen Avatar asked Jul 30 '10 08:07

Brandon Bertelsen


People also ask

How do I move a column to a row in R?

Thus, to convert columns of an R data frame into rows we can use transpose function t. For example, if we have a data frame df with five columns and five rows then we can convert the columns of the df into rows by using as. data. frame(t(df)).

How do I move a column to the front in R?

To move a column to first in the dataframe, we use relocate() with the column name we want to move. This will move the column of interest to the first column. We can also move the column of interest to a location after another column in the dataframe.


2 Answers

The subset function has a nice select argument that gives a convenient way to select ranges of columns by name:

df <- subset(df, select=c(g,a:f)) 
like image 52
Ken Williams Avatar answered Sep 21 '22 12:09

Ken Williams


I wrote this function recently called moveme. It's designed to work on vectors, with the intent of shuffling column orders around.

Here's the function:

moveme <- function (invec, movecommand) {   movecommand <- lapply(strsplit(strsplit(movecommand, ";")[[1]],                                   ",|\\s+"), function(x) x[x != ""])   movelist <- lapply(movecommand, function(x) {     Where <- x[which(x %in% c("before", "after", "first",                                "last")):length(x)]     ToMove <- setdiff(x, Where)     list(ToMove, Where)   })   myVec <- invec   for (i in seq_along(movelist)) {     temp <- setdiff(myVec, movelist[[i]][[1]])     A <- movelist[[i]][[2]][1]     if (A %in% c("before", "after")) {       ba <- movelist[[i]][[2]][2]       if (A == "before") {         after <- match(ba, temp) - 1       }       else if (A == "after") {         after <- match(ba, temp)       }     }     else if (A == "first") {       after <- 0     }     else if (A == "last") {       after <- length(myVec)     }     myVec <- append(temp, values = movelist[[i]][[1]], after = after)   }   myVec } 

Usage is simple. Try these out:

moveme(names(df), "g first") moveme(names(df), "g first; a last; e before c") 

Of course, using it to reorder the columns in your data.frame is straightforward:

df[moveme(names(df), "g first")] 

And for data.tables (moves by reference, no copy) :

setcolorder(dt, moveme(names(dt), "g first")) 

The basic options are:

  • first
  • last
  • before
  • after

Compounded moves are separated by a semicolon.

like image 23
A5C1D2H2I1M1N2O1R2T1 Avatar answered Sep 23 '22 12:09

A5C1D2H2I1M1N2O1R2T1