dt_matrix_format: Convert between demographic data stored in data.table and...

matrix_to_dtR Documentation

Convert between demographic data stored in data.table and matrix format

Description

Convert demographic data (can be by age and/or sex) between two data formats, [data.table()] and [matrix()]. When stored in matrix form the data is required to be square (ie. same number of ages in every year).

Usage

matrix_to_dt(
  mdt,
  year_right_most_endpoint,
  age_right_most_endpoint = Inf,
  gen_end_interval_col = TRUE,
  value_col = "value",
  validate_arguments = TRUE
)

dt_to_matrix(
  dt,
  id_cols = c("year_start", "year_end", "sex", "age_start", "age_end"),
  value_col = "value",
  validate_arguments = TRUE
)

Arguments

mdt

[data.table()]
Input demographic data in the matrix format described in the details section.

year_right_most_endpoint

[numeric(1)]
Assumed right most endpoint of the calendar year intervals. If mdt is actually data collected at a specific point in time (like census data) and not spanning intervals then assign 'NULL'.

age_right_most_endpoint

[numeric(1)]
Assumed right most endpoint of the age group intervals. Default is Inf.

gen_end_interval_col

[logical(1)]
Whether to automatically generate the end of the interval columns (like 'age'). Default is 'TRUE'.

value_col

[character(1)]
Name of the column containing the value of interest. Default is 'value'.

validate_arguments

[logical(1)]
Whether to validate that the input arguments are formatted correctly. Default is 'TRUE'.

dt

[data.table()]
Input demographic data in the data.table format described in the details section.

id_cols

[character()]
ID columns that uniquely identify each row of dt. This can only contain year, sex, and age variables. 'year_start' and 'year_end' must be included, and might contain 'sex' if the data is sex-specific, and 'age_start' and 'age_end' if the data is age-specific.

Details

data.table format: When data is in data.table format then it must have a column for 'year_start' and 'year_end', if the data is sex-specific it must have a column for 'sex', and if the data is age-specific it must have columns for 'age_start' and 'age_end.

matrix format: When data is in matrix format, columns represent the start of each calendar year interval, rows represent the start of each age interval. If the data is sex specific each sex will have a separate matrix stored in a named list (names corresponding to each sex). If the data is age specific rows will be labeled with a numeric representing the start of the age group.

Value

matrix_to_dt returns a [data.table()] in data.table format as described in the details section. dt_to_matrix returns a matrix of list of matrices as described in the details section.

Examples

output_matrix <- demCore:::dt_to_matrix(thailand_initial_estimates$survival)
output_dt <- demCore:::matrix_to_dt(output_matrix, year_right_most_endpoint = 2000)


ihmeuw-demographics/demCore documentation built on Feb. 24, 2024, 11:05 p.m.