Split data frame into fixed-size rows

I have a set of data frames with varying degrees of length, starting from approx. From 15,000 to 500,000. For each of these data frames, I would like to divide them into smaller data frames, each of which contains 300 rows, to which I will continue to process. How can i do this?

This ( Dividing a data frame by the number of rows ) gives a partial answer, but it does not work because not all of my data frames have a length multiple of 300.

It would be greatly appreciated if both plyr and non-plyr were provided.

Thanks!

+4
source share
2 answers

I do not understand why plyr solution is required. split works fine, and even hadley himself did not offer a plyr / reshape2 solution when he looked at an earlier question:

 split(dfrm, (0:nrow(dfrm) %/% 300) # modulo division 

Throws a warning, but since you were expecting an unevenly divisible result, you should ignore it.

+11
source

Something like the following might help

 numBreaks <- nrow(DAT)%/%300 + 1 for( i in seq(numBreaks)){ smallDAT <- DAT[((i-1)*300+1):(min(nrow(DAT), i*300)), ] ..... } 
+1
source

Source: https://habr.com/ru/post/1496079/


All Articles