gen: Create a new variable within the data frame

Description Usage Arguments Details Author(s) See Also Examples

View source: R/gen.R

Description

gen easily create a new variable within the data frame

Usage

1
gen(data, var, expr = NULL)

Arguments

data

data frame

var

name of a new variable

expr

value or Expression: See examples below.

Details

The value of the variable are specified by expr argument. If expr is not specified, gen generates NA by default.

Author(s)

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

See Also

egen

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
str(infert)

# convert age into months
df1 <- gen(infert, age.month, age * 12)
head(df1)

# age category
df2 <- gen(infert, age.cat, ifelse(age < 25, 1, ifelse(age >= 25 & age < 35, 2, 3)))
head(df2)

# age category
summary(infert$age)
df3 <- gen(infert, age.cat, cut(age, c(21, 35, 44)))
head(df3)

# create new variable with mean value
df4 <- gen(infert, age.mean, mean(age))
head(df4)

# new NA variable
df5 <- gen(infert, age.cat)
head(df5)

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