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

Description Usage Arguments Examples

View source: R/genFactor.R

Description

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

Usage

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

Arguments

dtName

Data table with column

varname

Name of field that is to be converted

labels

Factor level labels. If not provided, the generated factor levels will be used as the labels.

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

kgoldfeld/simstudy documentation built on Nov. 8, 2018, 7:41 p.m.