Merge two data.tables without modifying inputs

mergeData(dt1, dt2, idvars, na.rm = TRUE)

Arguments

dt1

First data.table

dt2

Second data.table

idvars

Character vector of column names to merge by

na.rm

Logical. If TRUE, performs an inner join (removing unmatched rows). If FALSE, performs a full outer join.

Value

A new merged data.table with the original key of dt1 preserved

Examples

def1 <- defData(varname = "x", formula = 0, variance = 1)
def1 <- defData(varname = "xcat", formula = ".3;.2", dist = "categorical")

def2 <- defData(varname = "yBin", formula = 0.5, dist = "binary", id = "xcat")
def2 <- defData(def2, varname = "yNorm", formula = 5, variance = 2)

dt1 <- genData(20, def1)
#> Warning: Probabilities do not sum to 1. Adding category to all rows!
dt2 <- genData(3, def2)

dtMerge <- mergeData(dt1, dt2, "xcat")
dtMerge
#> Key: <id>
#>        id  xcat  yBin    yNorm
#>     <int> <int> <int>    <num>
#>  1:     1     3     1 3.891610
#>  2:     2     2     1 3.666332
#>  3:     3     1     0 3.224902
#>  4:     4     2     1 3.666332
#>  5:     5     3     1 3.891610
#>  6:     6     3     1 3.891610
#>  7:     7     1     0 3.224902
#>  8:     8     2     1 3.666332
#>  9:     9     3     1 3.891610
#> 10:    10     2     1 3.666332
#> 11:    11     3     1 3.891610
#> 12:    12     1     0 3.224902
#> 13:    13     3     1 3.891610
#> 14:    14     2     1 3.666332
#> 15:    15     2     1 3.666332
#> 16:    16     3     1 3.891610
#> 17:    17     3     1 3.891610
#> 18:    18     1     0 3.224902
#> 19:    19     3     1 3.891610
#> 20:    20     3     1 3.891610
#>        id  xcat  yBin    yNorm