Creates a new variable by carrying forward selected responses from a base variable into a new variable, while preserving specified responses from an original variable.
carry_forward(
x,
parent_var,
child_var,
new_var,
orig_resp,
keep_resp,
verbose = TRUE
)A data frame or tibble.
A character string naming the variable whose selected responses should be carried forward.
A character string naming the original variable that corresponds to the new variable.
A character string naming the new variable to create.
A vector of responses in parent_var that should be carried
forward into new_var.
A vector of responses in child_var that should be kept
and not overwritten.
Logical. If TRUE, prints a summary of changes
The input data frame with a new variable added.
if (FALSE) { # \dontrun{
df <- data.frame(
q1 = c("Yes", "No", "DK", "Yes", NA),
q2 = c(NA, "Refused", "No", "Maybe", "Yes")
)
carry_forward(
x = df,
parent_var = "q1",
child_var = "q2",
new_var = "q2_cf",
orig_resp = c("Yes", "No"),
keep_resp = c("Refused", "Maybe")
)
} # }