Learn R Programming

mStats (version 3.4.0)

recode: Recode a variable

Description

recode() changes the values of a variable.

Usage

recode(data, var, ...)

Arguments

data

data.frame

var

variable name

...

specify in pattern: old value / new value.

Value

a data.frame

Details

It changes the values of a variable according to the old values specified. Values that does not meet any of the conditions, they are left unchanged.

Using colon : to indicate a range of numeric numbers

A numeric vector can be indicated by using : in old value. The function automatically filters the values that meet the range and assigns a specified new value to these.

Examples

Run this code
# NOT RUN {
x <- recode(infert, case, 0/"No", 1/"Yes")
tab(x, case)

# }
# NOT RUN {
## recode a factor
x <- recode(infert, education, "0-5yrs"/1, "6-11yrs"/2, "12+ yrs"/3)
tab(x, education)

## recode numeric vectors
x <- recode(infert, age, 21:28.9/1, 29:34.9/2, 35:44/3)
tab(x, age)

## recode NA
infert[4:20, "case"] <- NA
x <- recode(infert, case, NA/"Missing value")
tab(infert, case)
# }
# NOT RUN {
# }

Run the code above in your browser using DataLab