Description Usage Arguments Details Author(s) See Also Examples
gen
easily create a new variable within the data frame
1 |
data |
data frame |
var |
name of a new variable |
expr |
value or Expression: See examples below. |
The value of the variable are specified by expr
argument.
If expr
is not specified, gen
generates NA
by default.
Myo Minn Oo (Email: dr.myominnoo@gmail.com | Website: https://myominnoo.github.io/)
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)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.