Skip to contents

Calculates chunk indices of a data object for a given chunk size (number of items per chunk).


calc_chunks(x, size = 10, reverse = FALSE)



A data frame or vector.


An integer. The number of items (e.g. rows in a tibble) that make up a given chunk. Must be a positive integer. Caps out at data maximum.


A logical. Calculate chunks from back to front.


An iterable list of row indices for each chunk of data.


# Create chunk map for a data frame
chunks <- calc_chunks(mtcars, size = 6)

# Iterate through chunks of data
for (chunk in chunks) print(paste0(rownames(mtcars[chunk,]), collapse = ', '))
#> [1] "Mazda RX4, Mazda RX4 Wag, Datsun 710, Hornet 4 Drive, Hornet Sportabout, Valiant"
#> [1] "Duster 360, Merc 240D, Merc 230, Merc 280, Merc 280C, Merc 450SE"
#> [1] "Merc 450SL, Merc 450SLC, Cadillac Fleetwood, Lincoln Continental, Chrysler Imperial, Fiat 128"
#> [1] "Honda Civic, Toyota Corolla, Toyota Corona, Dodge Challenger, AMC Javelin, Camaro Z28"
#> [1] "Pontiac Firebird, Fiat X1-9, Porsche 914-2, Lotus Europa, Ford Pantera L, Ferrari Dino"
#> [1] "Maserati Bora, Volvo 142E"