egen: extension to gen function

Description Usage Arguments Details Author(s) See Also Examples

View source: R/egen.R

Description

egen transforms a numeric vector to a factor vector.

Usage

1
egen(x, cut = NULL, labels = NULL, na.rm = FALSE)

Arguments

x

numeric vector

cut

either a single number or a numeric vector.

labels

specify to name the factor levels.

na.rm

A logical value to specify missing values

Details

egen allows easy conversion of a numeric vector to factor.

Cut-off Intervals If the interval is not specified, it is cut at an interval of 10. Otherwise, it is divided into equal cut-off points by specified number.

Labelling If not specified, the labels are constructed in the format: variable name + "." + "cut-off intervals".

Author(s)

Myo Minn Oo (Email: dr.myominnoo@gmail.com | Website: https://myominnoo.github.io/)

See Also

gen

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
set.seed(1)
age <- round(c(rnorm(100, 45, 20), rep(NA, 20)),0)
summary(age)

egen(age)
egen(age, cut = 20)
egen(age, cut = c(1, 20, 40))
egen(age, cut = c(1, 20, 40), labels = c("young", "middle", "old"))
egen(age, cut = c(1, 20, 40, 60, 100))

## remove missing value
egen(age, cut = 20, na.rm = TRUE)
egen(age, cut = c(1, 20, 40), na.rm = TRUE)
egen(age, cut = c(1, 20, 40), labels = c("young", "middle", "old"), na.rm = TRUE)
egen(age, cut = c(1, 20, 40, 60, 100), na.rm = TRUE)

myominnoo/stats2 documentation built on Nov. 4, 2019, 8:33 p.m.