View source: R/interval_collapse.R
collapse_common_intervals | R Documentation |
Collapse an interval variable to the most detailed common set of
intervals available for each combination of id_cols
in a dataset.
Aggregates the collapsed dataset to the common set of intervals.
collapse_common_intervals(
dt,
id_cols,
value_cols,
col_stem,
agg_function = sum,
missing_dt_severity = "stop",
overlapping_dt_severity = "stop",
include_missing = FALSE
)
dt |
[ |
id_cols |
[ |
value_cols |
[ |
col_stem |
[ |
agg_function |
[ |
missing_dt_severity |
[ |
overlapping_dt_severity |
[ |
include_missing |
[ |
[data.table()
] with id_cols
and value_cols
columns but with
the col_stem
intervals reduced to only the most detailed common set of
intervals.
id_cols <- c("year_start", "year_end", "sex", "age_start", "age_end")
value_cols <- c("value")
# set up test input data.table
input_dt_male <- data.table::CJ(year_start = 2005, year_end = 2010,
sex = "male",
age_start = seq(0, 95, 5),
value = 25)
input_dt_male[age_start == 95, value := 5]
input_dt_female <- data.table::CJ(year_start = 2005:2009,
sex = "female",
age_start = seq(0, 95, 1),
value = 1)
gen_end(input_dt_female, setdiff(id_cols, c("year_end", "age_end")),
col_stem = "year", right_most_endpoint = 2010)
input_dt <- rbind(input_dt_male, input_dt_female)
gen_end(input_dt, setdiff(id_cols, "age_end"), col_stem = "age")
data.table::setkeyv(input_dt, id_cols)
collapsed_dt <- collapse_common_intervals(
dt = input_dt,
id_cols = id_cols,
value_cols = value_cols,
col_stem = "year"
)
collapsed_dt <- collapse_common_intervals(
dt = collapsed_dt,
id_cols = id_cols,
value_cols = value_cols,
col_stem = "age"
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.