life_table_prep | R Documentation |
Processes individual-level death data to create a standardized data table.
This table is collapsed/aggregated by age bin and optionally, by
demographics, for use with life_table
.
life_table_prep(
ph.data,
cuts = c(0, 1, 5, 10, 15, 18, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85),
dobvar = "date_of_birth",
dodvar = "date_of_death",
group_by = NULL
)
ph.data |
a data.table or data.frame. Must contain individual-level death data with the date of birth, date of death, and any demographics which you want to use to aggregate the resulting table. The default value is |
cuts |
integer vector of any length greater than 1 (typically of length ~ 20). It specifies the cut-points for the age groupings to be created in the data. Each number represents the beginning of an interval and the final cut extends through to the maximum age observed. For example, when cuts = c(0, 5, 10, 20), the data will be grouped into ages [0,5), [5,10), [10,20), and [20, infinity). The default is |
dobvar |
character vector of length one identifying a column with the decedent's date of birth. The referenced column must be of class 'date' or class 'character' in the format "YYYY-MM-DD" or "YYYY/MM/DD." The default is |
dodvar |
character vector of length one identifying a column with the decedent's date of death. The referenced column must be of class 'date' or class 'character' in the format "YYYY-MM-DD" or "YYYY/MM/DD." The default is |
group_by |
a character vector of indeterminate length. This is used to
specify all the variables by which you want to group (a.k.a. stratify) the
results. For example, if you specified The default is |
Note that population data (from get_population
) must be merged
onto the returned data.table before running it through
life_table
.
a data.table with deaths aggregated by any demographics specified in
the group_by
argument, as well as ages
(age group), deaths
(deaths per
demographic group and age group), and fraction
(the mean fraction of the
age interval lived by those who died in that interval).
# create data set ----
set.seed(98104)
deaths <- data.table::data.table(
date_of_death = as.Date("2020-01-01") + sample(0:365, 10000, replace = TRUE),
race_eth = rep_len(c("AIAN", "Asian", "Black", "Hispanic", "NHPI", "White"),
length.out = 10000),
gender = sample(c('Male', 'Female'), 10000, replace = TRUE),
year = 2020
)
# Calculate a date of birth based on a maximum age of 120 years (~43800 days)
deaths[, date_of_birth := date_of_death - sample(1:43800, 10000, replace = TRUE)]
# process with life_table_prep ----
output1 <- life_table_prep(ph.data = deaths)
head(output1)
# process with life_table_prep using group_by argument----
output2 <- life_table_prep(ph.data = deaths, group_by = c('gender', 'race_eth'))
head(output2)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.