equal_split: Split a set x into subset of nearly equal size.

Description Usage Arguments Details Value Author(s) Examples

Description

Split a set x stored in a vector or list into a number of maximally equally sized subsets.

Usage

1
equal_split(x, n, random = TRUE, beginning = FALSE)

Arguments

x

The set of value to be split into subsets, a vector or list.

n

The desired number of subsets.

random

Should the elements of the subsets be randomly sampled from x?

beginning

Should excess elements be added starting at the beginning of the subsets or randomly assigned?

Details

Note that if the number of desired subsets n is larger than the supplied set x, the output will still be a list with length n where length(x) entries will contain exactly one element and n - length(x) entries will be empty.

Value

A list containing the subsets. If set sizes are unequal, an attribute 'indices' is added and containes the indices of the subsets to which excess elements were added.

Author(s)

Domink Mueller (dominikmueller64@yahoo.de)

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
x <- seq.int(from = 1L, to = 10L)
n <- 3L
equal_split(x, n, random = TRUE, beginning = FALSE)
equal_split(x, n, random = TRUE, beginning = TRUE)
equal_split(x, n, random = FALSE, beginning = FALSE)
equal_split(x, n, random = FALSE, beginning = TRUE)

# lists as input work as well
equal_split(as.list(x), n, random = TRUE, beginning = FALSE)

# A too large number of desired subsets is handeled gracefully.
equal_split(x, n = 20L, random = TRUE, beginning = FALSE)

DominikMueller64/dmisc documentation built on May 6, 2019, 2:52 p.m.