valid_recode: Recode values to 0 and 1 according to predefined vector of...

Description Usage Arguments Value Examples

Description

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.

Usage

1
valid_recode(variable, valid.values, keep.na = FALSE)

Arguments

variable

The variable that should be recoded to 0 and 1, must be a vector.

keep.na

Logical, should missings be kept as NA? Default is to FALSE, returning 0 for a missing value.

valid_values

The vector of values that are valid, i. e. will be returned as 1.

Value

A vector containing 0 and 1 (and NA if specified) of type integer.

Examples

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)

kthorstmann/horst documentation built on May 20, 2019, 7:04 p.m.