quanteda (version 1.5.2)

sample_bygroup: Sample a vector by a group

Description

Return a sample from a vector within a grouping variable.

Usage

sample_bygroup(x, group, size = NULL, replace = FALSE)

Arguments

x

any vector

group

a grouping vector equal in length to length(x)

size

the number of items to sample within each group, as a positive number or a vector of numbers equal in length to the number of groups. If NULL, the sampling is stratified by group in the original group sizes.

replace

logical; should sampling be with replacement?

Value

x resampled within groups

Examples

Run this code
# NOT RUN {
set.seed(100)
grvec <- c(rep("a", 3), rep("b", 4), rep("c", 3))
quanteda:::sample_bygroup(1:10, group = grvec, replace = FALSE)
quanteda:::sample_bygroup(1:10, group = grvec, replace = TRUE)
quanteda:::sample_bygroup(1:10, group = grvec, size = 2, replace = TRUE)
quanteda:::sample_bygroup(1:10, group = grvec, size = c(1, 1, 3), replace = TRUE)
# }

Run the code above in your browser using DataLab