[R] Re-arrange Columns in data frame

Gabor Grothendieck ggrothendieck at gmail.com
Wed Nov 25 17:27:00 CET 2009


Try this:

# first case
ix <- c(matrix(1:24, 4, byrow = TRUE))
DF[ix]

# second case
ix <- c(matrix(1:16, 4, byrow = TRUE))
DF[ix]

On Wed, Nov 25, 2009 at 11:16 AM, Jabez Wilson <jabezwuk at yahoo.co.uk> wrote:
> Hi, I have a data frame which is 24 columns by 10 rows. This is essentially 6 groups of 4 columns. I want to re-arrange the columns into the following order 1,7,13,19,2,8,14,20,3,9,15,21,4,10,16,22,5,11,17,23,6,12,18,24 i.e. first of each group of 6 grouped together, then 2nd of each group of six etc.
> I know that I can do df[,c(1,7,13,19,2,8,14,20,3,9,15,21,4,10,16,22,5,11,17,23,6,12,18,24)], but what if I now have 4 groups of 4 columns, I would want the order to be c(1,5,9,13,2,6,10,14,3,7,11,15,4,8,12,16). I know that seq() comes into it somewhere, and I've got as far as seq(1,ncol(df),number_of_groups), but that gives me only one sequence. Is there a way of combining with rep() that can do this?
> Jabez
>
>
>
>        [[alternative HTML version deleted]]
>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>




More information about the R-help mailing list