egen: An extension to generate 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
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
egen(
  data = NULL,
  old.var,
  cut = NULL,
  lbl = NULL,
  new.var = NULL,
  na.rm = FALSE
)

## Default S3 method:
egen(
  data = NULL,
  old.var,
  cut = NULL,
  lbl = NULL,
  new.var = NULL,
  na.rm = FALSE
)

## S3 method for class 'data.frame'
egen(
  data = NULL,
  old.var,
  cut = NULL,
  lbl = NULL,
  new.var = NULL,
  na.rm = FALSE
)

Arguments

data

dataframe

old.var

variable to perform cut

cut

either a single number or a numeric vector.

lbl

specify to name the factor levels.

new.var

name of new variable generated

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

generate

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
## Not run: 
## variable with dataframe
# automatically categorized into interval of 10
infert.new <- egen(infert, age)
str(infert.new)
tab(age.cat, infert.new)

infert.new <- egen(infert, age, c(30, 35, 40))
str(infert.new)
tab(age.cat, infert.new)

infert.new <- egen(infert, age, c(30, 35, 40),
                c("young", "mid", "old", "oldest"))
str(infert.new)
tab(age.cat, infert.new)

# give variable name as age.grp
infert.new <- egen(infert, age, c(30, 35, 40),
                c("young", "mid", "old", "oldest"), age.grp)
str(infert.new)
tab(age.grp, infert.new)

# age.grp duplicates
infert.new1 <- egen(infert.new, age, c(30, 35, 40),
                c("young", "mid", "old", "oldest"), age.grp)

## End(Not run)

myominnoo/mStats_beta documentation built on Feb. 29, 2020, 8:17 a.m.