Create dummy variables from a factor or integer variable
genDummy(dtName, varname, sep = ".", replace = FALSE)
Data table with column
Name of factor
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.
If replace is set to TRUE (defaults to FALSE) the field referenced varname will be removed.
# 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
#> Key: <id>
#> id cat x
#> <int> <int> <num>
#> 1: 1 3 6.329660
#> 2: 2 3 8.149867
#> 3: 3 1 3.091388
#> 4: 4 2 4.931580
#> 5: 5 3 6.622817
#> ---
#> 196: 196 2 4.898831
#> 197: 197 2 3.910139
#> 198: 198 3 2.947997
#> 199: 199 1 2.871879
#> 200: 200 2 9.221231
dx <- genFactor(dx, "cat", labels = c("one", "two", "three"), replace = TRUE)
dx <- genDummy(dx, varname = "fcat", sep = "_")
dx
#> Key: <id>
#> id x fcat fcat_one fcat_two fcat_three
#> <int> <num> <fctr> <int> <int> <int>
#> 1: 1 6.329660 three 0 0 1
#> 2: 2 8.149867 three 0 0 1
#> 3: 3 3.091388 one 1 0 0
#> 4: 4 4.931580 two 0 1 0
#> 5: 5 6.622817 three 0 0 1
#> ---
#> 196: 196 4.898831 two 0 1 0
#> 197: 197 3.910139 two 0 1 0
#> 198: 198 2.947997 three 0 0 1
#> 199: 199 2.871879 one 1 0 0
#> 200: 200 9.221231 two 0 1 0
# Second example:
dx <- genData(15)
dx <- trtAssign(dtName = dx, 3, grpName = "arm")
dx <- genDummy(dx, varname = "arm")
dx
#> Key: <id>
#> id arm arm.1 arm.2 arm.3
#> <int> <int> <int> <int> <int>
#> 1: 1 3 0 0 1
#> 2: 2 3 0 0 1
#> 3: 3 1 1 0 0
#> 4: 4 2 0 1 0
#> 5: 5 1 1 0 0
#> 6: 6 3 0 0 1
#> 7: 7 1 1 0 0
#> 8: 8 2 0 1 0
#> 9: 9 2 0 1 0
#> 10: 10 1 1 0 0
#> 11: 11 2 0 1 0
#> 12: 12 3 0 0 1
#> 13: 13 2 0 1 0
#> 14: 14 1 1 0 0
#> 15: 15 3 0 0 1