Add multi-factorial data

addMultiFac(dtOld, nFactors, levels = 2, coding = "dummy", colNames = NULL)

Arguments

dtOld

data.table that is to be modified

nFactors

Number of factors (columns) to generate.

levels

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.

coding

String value to specify if "dummy" or "effect" coding is used. Defaults to "dummy".

colNames

A vector of strings, with a length of nFactors. The strings represent the name for each factor.

Value

A data.table that contains the added simulated data. Each new column contains an integer.

Examples

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
#> id x A B C #> 1: 1 -1.8796096 1 3 2 #> 2: 2 0.2918719 2 2 1 #> 3: 3 1.4042147 1 1 2 #> 4: 4 0.4055118 1 2 1 #> 5: 5 -0.2784421 1 3 3 #> --- #> 356: 356 1.8500257 2 3 1 #> 357: 357 0.2549942 1 2 2 #> 358: 358 -0.3719562 1 2 1 #> 359: 359 -0.8049113 2 3 2 #> 360: 360 0.1845554 1 1 3
DT[, .N, keyby = .(A, B, C)]
#> A B C N #> 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)]
#> Var1 Var2 Var3 N #> 1: 0 0 0 37 #> 2: 0 0 1 37 #> 3: 0 1 0 38 #> 4: 0 1 1 38 #> 5: 1 0 0 37 #> 6: 1 0 1 38 #> 7: 1 1 0 38 #> 8: 1 1 1 37