Home > OS >  How to split a dataframe into a given number of rows one by one in R
How to split a dataframe into a given number of rows one by one in R

Time:04-03

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. So first dataset consists of rows from 1-30, the second dataset consists of rows 31-60 and so on in strict order. Or if I want to split it into 4 parts, 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 . In other words, the first split dataset will be from 1-38, the second from 39-76 and similarly.

How can i perform it? Thank you advance.

CodePudding user response:

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
split(iris, (1:nrow(iris) - 1) %/% ceiling(nrow(iris) / 4)) |> sapply(nrow)
#  0  1  2  3 
# 38 38 38 36 
5 groups
split(iris, (1:nrow(iris) - 1) %/% ceiling(nrow(iris) / 5)) |> sapply(nrow)
#  0  1  2  3  4 
# 30 30 30 30 30 
7 groups
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 

CodePudding user response:

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))
  • Related