
Assign named group labels to filtered rows by count or proportion
Source:R/mutate_group_named.R
mutate_group_named.RdRandomly assigns each filtered row to one of the supplied group_names.
Group membership is determined either by explicit counts (how many animals
per group) or by proportions (relative fractions). The column is always
VARCHAR. Useful for farm assignment, treatment groups, line splitting, and
any situation where groups have user-defined string labels.
Pipe get_table("ind_meta") — optionally through dplyr::filter() — into
this function. Filtered primary keys are sorted ascending before shuffling,
giving a stable, reproducible starting point.
Usage
mutate_group_named(
tbl,
col_name,
group_names,
counts = NULL,
proportions = NULL,
prop_method = "exact",
include_leftover = FALSE,
underfill_action = "error",
seed = NULL,
overwrite = FALSE,
quiet = FALSE
)Arguments
- tbl
A
tidybreed_tablefromget_table()(optionally filtered).- col_name
Character scalar. Name of the group column to create or update. Must be a valid SQL identifier and not a reserved column.
- group_names
Character vector. Group labels. Must be the same length as
countsorproportions.- counts
Integer vector of animals per group. Mutually exclusive with
proportions.- proportions
Numeric vector of relative group fractions. Mutually exclusive with
counts.- prop_method
"exact"(default) uses the largest-remainder method for deterministic integer rounding;"random"draws each label independently.- include_leftover
Logical. When
sum(counts) < n_filtered(or when proportions sum to < n due to rounding), add the unassigned rows to the last group. DefaultFALSEleaves themNULL.- underfill_action
"error"(default),"proportional", or"sequential". Only applies whencountsis supplied andsum(counts) > n_filtered.- seed
Integer or
NULL. If supplied, sets the RNG seed before shuffling. Validation happens before the seed is set.- overwrite
Logical. If
FALSE(default), error when any filtered row already has a non-NULLvalue incol_name.- quiet
Logical. If
TRUE, suppress informational messages.
Counts vs proportions
Exactly one of counts or proportions must be supplied.
counts: integer vector, same length asgroup_names. Whensum(counts) > n_filtered,underfill_actiondecides what happens. Whensum(counts) < n_filtered, remaining rows are leftNULLunlessinclude_leftover = TRUE(which adds them to the last group).proportions: numeric vector, same length asgroup_names. Values must be positive and are normalized to sum to 1 withinsqrt(.Machine$double.eps)tolerance. Withprop_method = "exact"(default), the largest-remainder method converts proportions to integer counts, tie-breaking left-to-right. Withprop_method = "random", each animal is independently drawn from the distribution.
Underfill behavior (counts mode only)
When sum(counts) > n_filtered and underfill_action:
"error"(default): stop before sampling."proportional": rescale counts to fitn_filteredusing the largest-remainder method."sequential": fill groups in order until animals run out; later groups may be partially or fully empty.
Examples
if (FALSE) { # \dontrun{
# Assign females to two farms by count
pop <- pop |>
get_table("ind_meta") |>
dplyr::filter(sex == "F") |>
mutate_group_named(
col_name = "farm",
group_names = c("farm_A", "farm_B"),
counts = c(60L, 40L)
)
# Assign all individuals by proportion (60 / 40 split)
pop <- pop |>
get_table("ind_meta") |>
mutate_group_named(
col_name = "farm",
group_names = c("farm_A", "farm_B"),
proportions = c(0.6, 0.4)
)
# If available animals are fewer than requested counts, rescale proportionally
pop <- pop |>
get_table("ind_meta") |>
dplyr::filter(gen == 1L) |>
mutate_group_named(
col_name = "trt",
group_names = c("control", "high", "low"),
counts = c(50L, 50L, 50L),
underfill_action = "proportional"
)
} # }