Create dummy variables from a factor or integer variable

genDummy(dtName, varname, sep = ".", replace = FALSE)

Arguments

dtName

Data table with column

varname

Name of factor

sep

Character to be used in creating new name for dummy fields. Valid characters include all letters and "_". Will default to ".". If an invalid character is provided, it will be replaced by default.

replace

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

Examples

# 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
#> id cat x #> 1: 1 2 6.450340 #> 2: 2 3 7.922583 #> 3: 3 1 4.786523 #> 4: 4 3 8.716692 #> 5: 5 2 4.981055 #> --- #> 196: 196 3 4.778965 #> 197: 197 3 4.974389 #> 198: 198 2 2.618697 #> 199: 199 3 6.289800 #> 200: 200 3 6.357688
dx <- genFactor(dx, "cat", labels = c("one", "two", "three"), replace = TRUE) dx <- genDummy(dx, varname = "fcat", sep = "_") dx
#> id x fcat fcat_one fcat_two fcat_three #> 1: 1 6.450340 two 0 1 0 #> 2: 2 7.922583 three 0 0 1 #> 3: 3 4.786523 one 1 0 0 #> 4: 4 8.716692 three 0 0 1 #> 5: 5 4.981055 two 0 1 0 #> --- #> 196: 196 4.778965 three 0 0 1 #> 197: 197 4.974389 three 0 0 1 #> 198: 198 2.618697 two 0 1 0 #> 199: 199 6.289800 three 0 0 1 #> 200: 200 6.357688 three 0 0 1
# Second example: dx <- genData(15) dx <- trtAssign(dtName = dx, 3, grpName = "arm") dx <- genDummy(dx, varname = "arm") dx
#> id arm arm.1 arm.2 arm.3 #> 1: 1 1 1 0 0 #> 2: 2 2 0 1 0 #> 3: 3 2 0 1 0 #> 4: 4 1 1 0 0 #> 5: 5 2 0 1 0 #> 6: 6 1 1 0 0 #> 7: 7 3 0 0 1 #> 8: 8 2 0 1 0 #> 9: 9 1 1 0 0 #> 10: 10 3 0 0 1 #> 11: 11 3 0 0 1 #> 12: 12 1 1 0 0 #> 13: 13 3 0 0 1 #> 14: 14 2 0 1 0 #> 15: 15 3 0 0 1