egen {mStats}R Documentation

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

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 `##-##`.

Value

data.frame

Author(s)

Email: dr.myominnoo@gmail.com

Website: https://myominnoo.github.io/

Examples


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)

## End(Not run)


[Package mStats version 3.4.0 Index]