Skip to contents

Generate row indices for partitioning a matrix-like object

Usage

partition_indices(nrow, n_partitions = NULL, partition_size = NULL)

Arguments

nrow

Integer. Total number of rows in the object to be partitioned.

n_partitions

Integer. Number of (roughly) equal partitions. Mutually exclusive with `partition_size`.

partition_size

Integer. Target number of rows per partition. Mutually exclusive with `n_partitions`.

Value

A named list of integer vectors, each containing the row indices for one partition.

Details

This function returns indices only and does not hold any data in memory. Intended for use in loops or iterators where partitions are materialised one at a time. The remainder from unequal division is retained as a smaller final partition rather than distributed or discarded.

When n_partitions is used, partition sizes differ by at most one row, with larger partitions appearing first. When partition_size is used, all partitions except possibly the last will have exactly partition_size rows.

Examples

idx <- partition_indices(nrow = 20, n_partitions = 3)