recodes recodes the values of one or more variables in a data frame

recodes(data, vars, from, to)

Arguments

data

a data frame.

vars

character vector of variable names.

from

a vector of values or conditions (see Details).

to

a vector of replacement values.

Value

a data frame

Details

  • For each variable in the vars parameter, values are checked against the list of values in the from vector. If a value matches, it is replaced with the corresponding entry in the to vector.

  • Once a given observation's value matches a from value, it is recoded. That particular observation will not be recoded again by that recodes() statement (i.e., no chaining).

  • One or more values in the from vector can be an expression, using the dollar sign ($) to represent the variable being recoded. If the expression evaluates to TRUE, the corresponding to value is returned.

  • If the number of values in the to vector is less than the from vector, the values are recycled. This lets you convert several values to a single outcome value (e.g., NA).

  • If the to values are numeric, the resulting recoded variable will be numeric. If the variable being recoded is a factor and the to values are character values, the resulting variable will remain a factor. If the variable being recoded is a character variable and the to values are character values, the resulting variable will remain a character variable.

Note

See the vignette for a worked example.

Examples

if (FALSE) { # For variables A, B and C, convert 86, 99, and 999 to missing df <- recodes(df, vars=c("A", "B", "C"), from=c(86, 99, 999), to=NA) # For variables A, B, and C convert NA to 0 df <- recodes(df, vars=c("A", "B", "C"), from=NA, to=0) # For variable X1, X2, and X3 convert 1 to Yes and 2 to No df <- recodes(df, vars=c("X1", "X2", "X3"), from=c(1, 2), to=c("Yes", "No")) # For variable SEX convert m to Male and f to Female df <- recodes(df, vars="SEX", from=c("m", "f"), to=c("Male", "Female")) # For variable OUTCOME convert Live to 0 and Die to 1 df <- recodes(df, vars="OUTCOME", from=c("live", "die"), to=c(0, 1)) # For variable AGE convert age <= 20 to young, # 20 < age < =60 to middle, and age > 60 to old df <- recodes(df, vars="AGE", from=c("$ < = 20", "$ > 20 & $ <= 60", "$ > 60"), to=c("young", "middle", "old")) # Reverse code variable Rating from 1 to 10, to 10 to 1 df <- recodes(df, vars="Rating", from=1:10, to=10:1) }