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
)

Arguments

x

A data frame or tibble.

parent_var

A character string naming the variable whose selected responses should be carried forward.

child_var

A character string naming the original variable that corresponds to the new variable.

new_var

A character string naming the new variable to create.

orig_resp

A vector of responses in parent_var that should be carried forward into new_var.

keep_resp

A vector of responses in child_var that should be kept and not overwritten.

verbose

Logical. If TRUE, prints a summary of changes

Value

The input data frame with a new variable added.

Examples

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")
)
} # }