Learn R Programming

mStats (version 3.2.2)

egen: An extension to generate

Description

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

Usage

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

Arguments

data

Dataset

var

Exisitng variable

cut

either a single number or a numeric vector.

new_var

name of new variable to be generated

lbl

labels to specify

Details

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

Cut-off Intervals

If the interval is not specified, it is cut at an interval of 10, starting from the minimum value. Otherwise, it is divided into corresponding intervals by specified cut-off points.

Automatic Labelling

If lbl is not specified, labels are constructed in this format: lbl[##-##]

Examples

Run this code
# NOT RUN {
## use infert data

data(infert)
codebook(infert)
summ(infert, age)


## transform continuous to category
infert.new <- egen(infert, age, c(20, 30, 40, 50))
tab(infert.new, age.cat)

## specifiy labels and name
infert.new <- egen(infert, age, c(20, 30, 40), "age_cat", c("<30", "30-39", "40+"))
tab(infert.new, age_cat)


# }

Run the code above in your browser using DataLab