sample_each: Sample each

sample_eachR Documentation

Sample each

Description

Returns a logical vector where n items are randomly sampled from each unique value of a vector, x.

Usage

sample_each(x, n = 1L)

Arguments

x

a character, factor, or numeric vector

n

number to sample from each unique group in order; if x is a factor, n should correspond to levels(x); otherwise, n will be matched with the sorted unique groups

Value

A logical vector the same length as x identifying selected indices.

See Also

sample; kinda_sort

Examples

x <- mtcars$gear
sample_each(x)
mtcars[sample_each(x), ]


## compare numeric vs factor vectors (see description above)
## samples 3 3s, 4 4s, 5 5s
table(mtcars[sample_each(x, 3:5), ]$gear)

## samples 3 5s, 4 4s, 5 3s
X <- factor(x, 5:3)
table(mtcars[sample_each(X, 3:5), ]$gear)


raredd/rawr documentation built on March 4, 2024, 1:36 a.m.