Add multi-factorial data
addMultiFac(dtOld, nFactors, levels = 2, coding = "dummy", colNames = NULL)
data.table that is to be modified
Number of factors (columns) to generate.
Vector or scalar. If a vector is specified, it must be the same length as nFatctors. Each value of the vector represents the number of levels of each corresponding factor. If a scalar is specified, each factor will have the same number of levels. The default is 2 levels for each factor.
String value to specify if "dummy" or "effect" coding is used. Defaults to "dummy".
A vector of strings, with a length of nFactors. The strings represent the name for each factor.
A data.table that contains the added simulated data. Each new column contains an integer.
defD <- defData(varname = "x", formula = 0, variance = 1)
DT <- genData(360, defD)
DT <- addMultiFac(DT, nFactors = 3, levels = c(2, 3, 3), colNames = c("A", "B", "C"))
DT
#> Key: <id>
#> id x A B C
#> <int> <num> <int> <int> <int>
#> 1: 1 1.33037679 1 3 3
#> 2: 2 -0.53549641 2 2 2
#> 3: 3 -0.91155077 2 3 1
#> 4: 4 -0.21853793 2 1 2
#> 5: 5 0.93554171 2 3 2
#> ---
#> 356: 356 -0.75060380 1 2 1
#> 357: 357 -0.62399716 1 1 1
#> 358: 358 -0.87007269 1 2 2
#> 359: 359 -0.01525919 2 3 3
#> 360: 360 -1.14390972 1 1 3
DT[, .N, keyby = .(A, B, C)]
#> Key: <A, B, C>
#> A B C N
#> <int> <int> <int> <int>
#> 1: 1 1 1 20
#> 2: 1 1 2 20
#> 3: 1 1 3 20
#> 4: 1 2 1 20
#> 5: 1 2 2 20
#> 6: 1 2 3 20
#> 7: 1 3 1 20
#> 8: 1 3 2 20
#> 9: 1 3 3 20
#> 10: 2 1 1 20
#> 11: 2 1 2 20
#> 12: 2 1 3 20
#> 13: 2 2 1 20
#> 14: 2 2 2 20
#> 15: 2 2 3 20
#> 16: 2 3 1 20
#> 17: 2 3 2 20
#> 18: 2 3 3 20
DT <- genData(300, defD)
DT <- addMultiFac(DT, nFactors = 3, levels = 2)
DT[, .N, keyby = .(Var1, Var2, Var3)]
#> Key: <Var1, Var2, Var3>
#> Var1 Var2 Var3 N
#> <num> <num> <num> <int>
#> 1: 0 0 0 38
#> 2: 0 0 1 37
#> 3: 0 1 0 37
#> 4: 0 1 1 38
#> 5: 1 0 0 38
#> 6: 1 0 1 38
#> 7: 1 1 0 37
#> 8: 1 1 1 37