Split up a data frame and store into a list?

I have a data frame with 48 columns. I want to split them into chunks of 12 (in this case giving me 3 data frames). I want to store the resulting data frames into a list. Would using a for loop be the best solution here since sometimes I have data frames with 60/72/84/96/etc columns?

mylist <- list()

mydf <- data.frame(t(seq(1,48)))

#Unsure how to proceed here!!
for(i in ncol(mydf)){
mylist[[i]] <- ???

}

chisel <- function(x,y,z) x[,y[[z]]]

chunk <- function(x,n) split(x, cut(seq_along(x), n, labels = FALSE)) 

mydf <- data.frame(t(seq(1,48)))

indices <- chunk(1:48,4)

holder <- list()

for(i in 1:4) holder[[i]] = chisel(mydf,indices,i)

holder
#> [[1]]
#>   X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 X11 X12
#> 1  1  2  3  4  5  6  7  8  9  10  11  12
#> 
#> [[2]]
#>   X13 X14 X15 X16 X17 X18 X19 X20 X21 X22 X23 X24
#> 1  13  14  15  16  17  18  19  20  21  22  23  24
#> 
#> [[3]]
#>   X25 X26 X27 X28 X29 X30 X31 X32 X33 X34 X35 X36
#> 1  25  26  27  28  29  30  31  32  33  34  35  36
#> 
#> [[4]]
#>   X37 X38 X39 X40 X41 X42 X43 X44 X45 X46 X47 X48
#> 1  37  38  39  40  41  42  43  44  45  46  47  48

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.