简体   繁体   English

如何将一个dataframe在R中一一拆分成给定行数

[英]How to split a dataframe into a given number of rows one by one in R

Suppose here the data.假设这里的数据。

data(iris)

Iris has 150 rows, how to split it on 5 datasets but with respect to the order of the rows.150/5=30. Iris 有 150 行,如何在 5 个数据集上拆分它,但相对于行的顺序。150/5=30。 So first dataset consists of rows from 1-30, the second dataset consists of rows 31-60 and so on in strict order.因此,第一个数据集由 1-30 行组成,第二个数据集由 31-60 行组成,依此类推。 Or if I want to split it into 4 parts, 150/4=37.5.或者如果我想把它分成 4 个部分,150/4=37.5。 This is a fractional number, so in such cases we round it up to the upper limit = 38. 38+38+38+36=150.这是一个小数,所以在这种情况下我们将它四舍五入到上限 = 38。 38+38+38+36=150。 In other words, the first split dataset will be from 1-38, the second from 39-76 and similarly.换句话说,第一个拆分数据集将从 1-38 开始,第二个拆分数据集将从 39-76 开始,类似地。

How can i perform it?我该如何执行? Thank you advance.谢谢你提前。

You can use something like this:你可以使用这样的东西:

num_groups <- 4
split(iris, (1:nrow(iris) - 1) %/% ceiling(nrow(iris) / num_groups))

Confirming above works for different group sizes:确认以上适用于不同的组大小:

4 groups 4组
split(iris, (1:nrow(iris) - 1) %/% ceiling(nrow(iris) / 4)) |> sapply(nrow)
#  0  1  2  3 
# 38 38 38 36 
5 groups 5组
split(iris, (1:nrow(iris) - 1) %/% ceiling(nrow(iris) / 5)) |> sapply(nrow)
#  0  1  2  3  4 
# 30 30 30 30 30 
7 groups 7组
split(iris, (1:nrow(iris) - 1) %/% ceiling(nrow(iris) / 7)) |> sapply(nrow)
#  0  1  2  3  4  5  6 
# 22 22 22 22 22 22 18 

Another possible solution:另一种可能的解决方案:

library(tidyverse)

data(iris)

ngroups <- 4
v <- 1:nrow(iris)

split(v, ceiling(seq_along(v) / round(nrow(iris)/ngroups))) %>% 
  map(~ slice(iris, .x))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM