Learn R Programming

mStats (version 3.4.0)

egen: Categorize a numerical variable

Description

egen() transforms a numeric vector to a factor vector.

Usage

egen(data, var, cut = NULL, lbl = NULL, new_var = NULL)

Arguments

data

data.frame

var

existing variable

cut

either a number or a numeric vector

lbl

labels to specify

new_var

name of new variable to be created

Value

data.frame

Details

egen allows easy conversion of a numerical variable to a categorical variable.

If only a number is specified in cut, it categorizes into equal intervals based on that number. If no value is set for cut, the default interval is 10.

Automatic naming new variable

If new_var is not specified, new names will be automatically created by appending _cat as suffix. VARNAME`_cat`

Automatic Labelling

If lbl is not specified, labels are constructed in `##-##`.

Examples

Run this code
# NOT RUN {
x <- egen(infert, age)
tab(x, age_cat)

# }
# NOT RUN {
## Set cut-off points
x <- egen(infert, age, c(26, 31, 36, 41))
tab(x, age_cat)

## Add labels and give a new name
x <- egen(infert, age, c(26, 31, 36, 41),
         lbl = c("<= 25", "26 - 30", "31 - 35",
           "36 - 40", "41+"),
         new_var = age_grp)
tab(x, age_grp)
# }
# NOT RUN {
# }

Run the code above in your browser using DataLab