Read external csv data set definitions for adding columns

defReadCond(filen)

Arguments

filen

String file name, including full path. Must be a csv file.

Value

A data.table with data set definitions

See also

[distributions]

Examples

# Create temporary external "csv" files

test1 <- c(
  "varname,formula,variance,dist,link",
  "x,0.3;0.4;0.3,0,categorical,identity"
)

tfcsv1 <- tempfile()
writeLines(test1, tfcsv1)

test2 <- c(
  "condition,formula,variance,dist,link",
  "x == 1, 0.4,0,binary,identity",
  "x == 2, 0.6,0,binary,identity",
  "x >= 3, 0.8,0,binary,identity"
)

tfcsv2 <- tempfile()
writeLines(test2, tfcsv2)

# Generate data based on external definitions

defs <- defRead(tfcsv1)
dt <- genData(2000, defs)
dt
#> Key: <id>
#>          id     x
#>       <int> <int>
#>    1:     1     1
#>    2:     2     2
#>    3:     3     1
#>    4:     4     3
#>    5:     5     2
#>   ---            
#> 1996:  1996     2
#> 1997:  1997     2
#> 1998:  1998     1
#> 1999:  1999     3
#> 2000:  2000     1

# Add column based on

defsCond <- defReadCond(tfcsv2)
dt <- addCondition(defsCond, dt, "y")
dt
#> Key: <id>
#>          id     y     x
#>       <int> <int> <int>
#>    1:     1     1     1
#>    2:     2     0     2
#>    3:     3     0     1
#>    4:     4     0     3
#>    5:     5     0     2
#>   ---                  
#> 1996:  1996     0     2
#> 1997:  1997     1     2
#> 1998:  1998     0     1
#> 1999:  1999     1     3
#> 2000:  2000     1     1

dt[, mean(y), keyby = x]
#> Key: <x>
#>        x        V1
#>    <int>     <num>
#> 1:     1 0.3982143
#> 2:     2 0.6072727
#> 3:     3 0.8243902

unlink(tfcsv1)
unlink(tfcsv2)