sample_each | R Documentation |
Returns a logical vector where n
items are randomly sampled from
each unique value of a vector, x
.
sample_each(x, n = 1L)
x |
a character, factor, or numeric vector |
n |
number to sample from each unique group in order; if |
A logical vector the same length as x
identifying selected indices.
sample
; kinda_sort
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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.