Calculates a named selection index by multiplying each individual's values by
the index weights defined in define_index() and summing the products.
Results are appended to the ind_index table with an auto-incrementing
index_number so that successive runs are preserved.
The first argument must be a tidybreed_table obtained from get_table()
(optionally filtered). The table must contain id_ind, a trait/phenotype
name column, and a numeric value column (value_col). For ind_phenotype
the name column is phenotype_name; for every other table (ind_ebv,
ind_tbv, or a user-defined table) it is trait_name — this is detected
automatically from the table's columns. Works with ind_ebv (EBVs),
ind_phenotype (phenotypes), ind_tbv (TBVs), or any user-defined table
with the same structure.
Usage
add_index(
tbl,
index_name,
value_col = NULL,
overwrite_index = FALSE,
delete_all = FALSE,
...
)Arguments
- tbl
A
tidybreed_tablefromget_table()(optionally filtered). Must containid_ind, a name column (trait_name, orphenotype_nameforind_phenotype), and the column specified byvalue_col. Any table with this structure is accepted:ind_ebv,ind_phenotype,ind_tbv, or a custom table.- index_name
Character scalar. Name of the index to compute; must already exist in
index_meta(created viadefine_index()).- value_col
Character scalar or
NULL. The column intblthat holds the numeric value to weight. WhenNULL(default), auto-detected from the table name:ind_ebv→"ebv_value",ind_phenotype→"pheno_value",ind_tbv→"tbv_value". An error is thrown for unknown tables ifvalue_colis not supplied.- overwrite_index
Logical. If
TRUE, all existingind_indexrows for thisindex_nameare deleted before inserting; new rows receiveindex_number = 1. DefaultFALSE.- delete_all
Logical. If
TRUE, all rows inind_indexare deleted before inserting; new rows receiveindex_number = 1. Takes precedence overoverwrite_index. DefaultFALSE.- ...
Optional extra columns to add to
ind_index. Scalar values only (broadcast to all inserted rows). Types are inferred viainfer_duckdb_type().
Examples
if (FALSE) { # \dontrun{
# Compute index from EBVs — filter to a specific model and eval_number
pop <- pop |>
get_table("ind_ebv") |>
dplyr::filter(model == "blup_v1", eval_number == 1L) |>
add_index("terminal")
# Compute phenotypic index — filter to first record per individual per trait
pop <- pop |>
get_table("ind_phenotype") |>
dplyr::filter(pheno_number == 1L) |>
add_index("terminal")
# Compute true-value index from TBVs (value_col auto-detected)
pop <- pop |>
get_table("ind_tbv") |>
add_index("terminal")
# Explicit value_col for a user-defined table
pop <- pop |>
get_table("my_scores") |>
add_index("terminal", value_col = "composite_score")
# Replace previous run with a fresh one
pop <- pop |>
get_table("ind_ebv") |>
dplyr::filter(model == "blup_v2", eval_number == 1L) |>
add_index("terminal", overwrite_index = TRUE)
} # }
