Split Data Frame into Rows of Fixed Size

那年仲夏 提交于 2019-12-28 23:37:52

问题


I have a bunch of data frames with varying degrees of length, ranging from approx. 15,000 to 500,000. For each of these data frames, I would like to split them up into smaller data frames each with 300 rows which I would do further processing on. How can I do this?

This (Split up a dataframe by number of rows) provides a partial answer, but it doesn't work because not all my data frames have length that are multiples of 300.

Would greatly appreciate it if a plyr and non-plyr solution can both be provided.

Thank you!


回答1:


I don't understand why a plyr solution is needed. split works perfectly well and even hadley himself didn't suggest a plyr/reshape2 solution when he looked at the earlier question:

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

Does produce a warning but since you were expecting a non-evenly divisible result you should ignore it.




回答2:


Something like the following may help

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


来源:https://stackoverflow.com/questions/18139708/split-data-frame-into-rows-of-fixed-size

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!