Split the list of data.frames into sublists data.frames

I have a list of data.frames that looks like this:

List          
[[1]]                
    .....               
  List             
   [[2]]            
  ....           

 List              
[[95]]   

      

I would like to split this long list of data.frames into sublists of 3 data.frames each to do other calculations in a simple and easy way.

Something like:

sublist1 <- List [1: 3]
sublist2 <- List [3: 6]
sublist3 <- List [6: 9]

etc.

+3


source to share


2 answers


I would do something like this:

ll <- by(seq_along(l),cut(seq_along(l),3),
                 FUN=function(x)l[x])

      

Now I have a list containing 3 lists. For example, to access the first sub-lists, you can:



ll[[1]]
[[1]]
data frame with 0 columns and 0 rows

[[2]]
data frame with 0 columns and 0 rows

[[3]]
data frame with 0 columns and 0 rows

      

And so on, ll [[2]] ...

+3


source


You can use assign

and do something like this:



d <- data.frame()
l <- list(d,d,d,d,d,d,d,d,d)

for(i in seq(1, length(l), by=3)) {
    assign(paste0("x", i), l[i:(i+2)])
}

> ls()
# [1] "d"  "i"  "l"  "x1" "x4" "x7"

      

+2


source







All Articles