genFactor: Create factor variable from an existing (non-double) variable

Description Usage Arguments Examples

View source: R/generate_data.R

Description

Create factor variable from an existing (non-double) variable

Usage

1
genFactor(dtName, varname, labels = NULL, prefix = "f", replace = FALSE)

Arguments

dtName

Data table with columns.

varname

Name of field(s) to be converted.

labels

Factor level labels. If not provided, the generated factor levels will be used as the labels. Can be a vector (if only one new factor or all factors have the same labels) or a list of character vectors of the same length as varname.

prefix

By default, the new field name will be a concatenation of "f" and the old field name. A prefix string can be provided.

replace

If replace is set to TRUE (defaults to FALSE) the field referenced varname will be removed.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
# First example:

def <- defData(varname = "cat", formula = ".2;.3;.5", dist = "categorical")
def <- defData(def, varname = "x", formula = 5, variance = 2)

dx <- genData(200, def)
dx

dx <- genFactor(dx, "cat", labels = c("one", "two", "three"))
dx

# Second example:

dx <- genData(10)
dx <- trtAssign(dtName = dx, 2, grpName = "studyArm")
dx <- genFactor(dx, varname = "studyArm", labels = c("control", "treatment"), prefix = "t_")
dx

simstudy documentation built on Oct. 23, 2020, 6:55 p.m.