[R] Re-arrange Columns in data frame
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@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.
Re: [R] Re-arrange Columns in data frame
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 jabez...@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@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. __ R-help@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.
Re: [R] Re-arrange Columns in data frame
Thanks very much. Using the matrix function and then DF[,ix] gives me exactly what I wanted. Jabez --- On Wed, 25/11/09, Gabor Grothendieck ggrothendi...@gmail.com wrote: From: Gabor Grothendieck ggrothendi...@gmail.com Subject: Re: [R] Re-arrange Columns in data frame To: Jabez Wilson jabez...@yahoo.co.uk Cc: R Mailing List r-help@r-project.org Date: Wednesday, 25 November, 2009, 16:27 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 jabez...@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@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. [[alternative HTML version deleted]] __ R-help@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.