Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I split a data frame among columns, say at every nth column?

Tags:

split

dataframe

r

Let's say I am working with the following data frame:

Mydata <- data.frame(X1 = c(1,2,3,4,5,6,7,8,9,10),X2 = c(1,3,1,1,1,5,1,1,8,1),
                     X3 = c(1,2,3,4,5,6,7,8,9,10),X4 = c(1,3,1,1,1,5,1,1,8,1),
                     X5 = c(1,2,3,4,5,6,7,8,9,10),X6 = c(1,3,1,1,1,5,1,1,8,1),
                     X7 = c(1,2,3,4,5,6,7,8,9,10),X8 = c(1,3,1,1,1,5,1,1,8,1),
                     X9 = c(1,2,3,4,5,6,7,8,9,10),X10 = c(1,3,1,1,1,5,1,1,8,1),
                     X11 = c(1,2,3,4,5,6,7,8,9,10),X12 = c(1,3,1,1,1,5,1,1,8,1))

I want to split this data frame into three separate data frames, with columns X1 ~ X4 in one data frame, X5 ~ X8 in the second, and X9 ~ X12. How would I code this to continue this pattern for any number of columns?

like image 286
Arnav Harkenavvy Avatar asked Jul 26 '18 19:07

Arnav Harkenavvy


1 Answers

To split Mydata every 4 columns. We can use explicitly use split.default:

split.default(Mydata, rep(1:3, each = 4))

The "default" method can split a data frame by columns. Just set the grouping variable by your need.


For balanced grouping, gl is handy (see ?gl). We can use gl(3, 4) instead of rep(1:3, 4) in the above, which avoids type conversion from "integer" to "factor".

In general, use gl(ncol(Mydata) / n, n) for "every n columns" (n must divide ncol(Mydata)).

like image 75
Zheyuan Li Avatar answered Oct 13 '22 14:10

Zheyuan Li