Creates a step that renames variables in the survey data when baked.
Usage
step_rename(
svy,
...,
mapping = NULL,
.copy = use_copy_default(),
comment = "Rename variables",
use_copy = deprecated(),
lazy = lazy_default(),
record = TRUE
)Arguments
- svy
A Survey or RotativePanelSurvey object
- ...
Pairs in the form
new_name = old_name(unquoted).- mapping
A named character vector of the form
c(new_name = "old_name"). Alternative to...for programmatic use.- .copy
Whether to operate on a copy (default:
use_copy_default())- comment
Descriptive text for the step for documentation and traceability (default
"Rename variables").- use_copy
- lazy
Internal. Whether to delay execution (default
lazy_default()).- record
Internal. Whether to record the step (default
TRUE).
Details
Lazy evaluation (default): By default, steps are recorded but
not executed until bake_steps() is called.
Variables can be renamed in two ways:
Unquoted pairs:
step_rename(svy, new_name = old_name)Named character vector:
step_rename(svy, mapping = c(new_name = "old_name"))
Variables that don't exist in the data cause an error, unlike
step_remove() which issues a warning.
See also
Other steps:
bake_steps(),
get_steps(),
step_compute(),
step_filter(),
step_join(),
step_recode(),
step_remove(),
step_validate(),
view_graph()
Examples
dt <- data.table::data.table(
id = 1:5, age = c(25, 30, 45, 50, 60),
w = rep(1, 5)
)
svy <- Survey$new(
data = dt, edition = "2023", type = "ech",
psu = NULL, engine = "data.table", weight = add_weight(annual = "w")
)
svy2 <- step_rename(svy, edad = age)
svy2 <- bake_steps(svy2)
"edad" %in% names(get_data(svy2)) # TRUE
#> [1] TRUE