Learn R Programming

BBmisc (version 1.5)

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, shuffle = FALSE)

Arguments

x
[vector | list] Vector or list to chunk.
chunk.size
[integer(1)] Requested number of elements in each chunk. Cannot be used in combination with n.chunks. If x cannot be evenly chunked, some chunks will have 1 element less.
n.chunks
[integer(1)] Requested number of chunks. Can not be used in combination with chunks.size
shuffle
[logical(1)] Shuffle x? Default is FALSE.

Value

  • [unnamed list] of chunks.

Examples

Run this code
xs <- 1:10
chunk(xs, chunk.size=3)
chunk(xs, n.chunks=2)
chunk(xs, n.chunks=2, shuffle=TRUE)

Run the code above in your browser using DataLab