Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

chunk

Chunk elements of vectors into blocks of nearly equal size.


Description

In case of shuffling and vectors that cannot be chunked evenly, it is chosen randomly which levels / chunks will receive 1 element less. If you do not shuffle, always the last chunks will receive 1 element less.

Usage

chunk(x, chunk.size, n.chunks, props, shuffle = FALSE)

Arguments

x

[ANY]
Vector, list or other type supported by split.

chunk.size

[integer(1)]
Requested number of elements in each chunk. Cannot be used in combination with n.chunks or props. If x cannot be evenly chunked, some chunks will have less elements.

n.chunks

[integer(1)]
Requested number of chunks. If more chunks than elements in x are requested, empty chunks are dropped. Can not be used in combination with chunks.size or props.

props

[numeric]
Vector of proportions for chunk sizes. Empty chunks may occur, depending on the length of x and the given proportions. Cannot be used in combination with chunks.size or n.chunks.

shuffle

[logical(1)]
Shuffle x? Default is FALSE.

Value

[unnamed list] of chunks.

Examples

xs = 1:10
chunk(xs, chunk.size = 3)
chunk(xs, n.chunks = 2)
chunk(xs, n.chunks = 2, shuffle = TRUE)
chunk(xs, props = c(7, 3))

BBmisc

Miscellaneous Helper Functions for B. Bischl

v1.11
BSD_2_clause + file LICENSE
Authors
Bernd Bischl [aut, cre], Michel Lang [aut], Jakob Bossek [aut], Daniel Horn [aut], Jakob Richter [aut], Dirk Surmann [aut]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.