recodes {qacBase} | R Documentation |
Recode one or more variables
Description
recodes
recodes the values of one or more variables in
a data frame
Usage
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. |
Details
For each variable in the
vars
parameter, values are checked against the list of values in thefrom
vector. If a value matches, it is replaced with the corresponding entry in theto
vector.Once a given observation's value matches a
from
value, it is recoded. That particular observation will not be recoded again by thatrecodes()
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 toTRUE
, the correspondingto
value is returned.If the number of values in the
to
vector is less than thefrom
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 theto
values are character values, the resulting variable will remain a factor. If the variable being recoded is a character variable and theto
values are character values, the resulting variable will remain a character variable.
Value
a data frame
Note
See the vignette for detailed examples.
Examples
df <- data.frame(x = c(1, 5, 7, 3, 0),
y = c(9, 0, 5, 9, 2),
z = c(1, 1, 2, 2, 1)
)
df <- recodes(df,
vars = c("x", "y"),
from = 0, to = NA)
df <- recodes(df,
vars = "z",
from = c(1, 2), to = c("pass", "fail"))