Expands data.table to dimensions not already present, leaving value columns NA.

expand(dt, expand_cols, id_cols)

Arguments

dt

[data.table()]
Data set you would like to expand.

expand_cols

[list()]
A named list of vectors, where the name of each vector is a column of dt that you would like to expand (or a new column name) and the value within the vector are the values you want the column to take. Ex: expand_cols = list(draw = 1:100) would mean you want the returned data.table to have draws 1 through 100.

id_cols

[characher()]
Columns which uniquely identify rows of dt. These columns will be copied over into the expanded rows.

Value

dt with added rows for all combinations of expand_cols as specified. In new rows, entries of non-ID columns will be NA.

Details

This function is a wrapper for data.table::CJ().

Syntax for example: dt[CJ(year = 1990:2000, draw = c(1:2), location = "Mexico", unique = T), on = list(location, year)]

Examples

dt <- data.table::data.table( location = c("Mexico"), year = c(1990:1995), value = c(0:5) ) dt <- expand(dt, expand_cols = list(year = 1990:2000, draw = c(1:2)), id_cols = c("location", "year"))