sample_group() randomly samples a specified number of unique values from a
grouping column and returns all rows belonging to the sampled groups.
Usage
sample_group(data, group_col, n)
Arguments
- data
A data.frame, tibble, or data.table.
- group_col
Unquoted column name used to define groups.
- n
Integer; number of distinct groups to sample. If greater than the
number of available groups, all groups are returned.
Value
An object of the same class as .data (tibble -> tibble,
data.table -> data.table, data.frame -> data.frame) containing only rows
whose group value was sampled.
Examples
df <- tibble::tibble(id = 1:8, grp = rep(letters[1:4], each = 2))
sample_group(df, grp, 2)
#> # A tibble: 4 × 2
#> id grp
#> <int> <chr>
#> 1 1 a
#> 2 2 a
#> 3 3 b
#> 4 4 b