Description Usage Arguments Value Examples
The function recodes a vector of elements into 0 and 1, based on a vector of valid values. It assumes that any correct answer in a questionnaire will be coded as 1 and all other answers (per default, even NA
) will be returned as 0.
1 | valid_recode(variable, valid.values, keep.na = FALSE)
|
variable |
The variable that should be recoded to 0 and 1, must be a vector. |
keep.na |
Logical, should missings be kept as |
valid_values |
The vector of values that are valid, i. e. will be returned as 1. |
A vector containing 0 and 1 (and NA
if specified) of type integer
.
1 2 3 4 5 6 7 8 9 | data <- data.frame(var1 = c("7", NA, "five", "nine", "eight", "six",
"x", "X", "100", "50", " five", "fife",
"5", " Five"),
var2 = c(1, 2, 3, 4, 1, NA, 9, 1, 14, 15, 0, 1, 3, 6),
id = c(1:14),
stringsAsFactors = FALSE)
valid_recode(variable = data$var1,
valid.values = c("five", "Five", "5"))
valid_recode(variable = data$var2, valid.values = c(1, 2), keep.na = TRUE)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.