🔁 map_column(): Map values in a column using named vector or list
Source:R/map_column.R
map_column.Rd
Maps values in a column of a data.frame (query) to new values using a named
vector or list (map
), optionally creating a new column or replacing the original.
Usage
map_column(
query,
by,
map,
to = "mapped",
overwrite = FALSE,
default = "unknown",
preview = TRUE
)
Arguments
- query
A data.frame containing the column to be mapped.
- by
A string. Column name in
query
to be mapped.- map
A named vector or list. Names are original values, values are mapped values.
- to
A string. Name of the column to store mapped results (if
overwrite = FALSE
).- overwrite
Logical. Whether to replace the
by
column with mapped values. Default: FALSE.- default
Default value to assign if no match is found. Default: "unknown".
- preview
Logical. Whether to print preview of result (default TRUE).
Examples
df <- data.frame(gene = c("TP53", "BRCA1", "EGFR", "XYZ"))
gene_map <- c("TP53" = "Tumor suppressor", "EGFR" = "Oncogene")
map_column(df, by = "gene", map = gene_map, to = "label")
#> Error in map_column(df, by = "gene", map = gene_map, to = "label"): could not find function "map_column"