问题描述
我有一堆长度不同的数据帧,范围从大约.15,000 到 500,000.对于这些数据帧中的每一个,我想将它们分成更小的数据帧,每个数据帧有 300 行,我将对其进行进一步处理.我该怎么做?
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?
这(按数量拆分数据帧行) 提供了部分答案,但它不起作用,因为并非所有数据帧的长度都是 300 的倍数.
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.
如果可以同时提供 plyr 和非 plyr 解决方案,我们将不胜感激.
Would greatly appreciate it if a plyr and non-plyr solution can both be provided.
谢谢!
推荐答案
我不明白为什么需要 plyr 解决方案.split
运行得非常好,甚至 hadley 在查看之前的问题时也没有提出 plyr/reshape2 解决方案:
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.
这篇关于将数据帧拆分为固定大小的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!