-
Notifications
You must be signed in to change notification settings - Fork 1
/
lifetable_function.R
43 lines (40 loc) · 1.74 KB
/
lifetable_function.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#' Lifetable Function
#'
#' Gives user more control over their lifetable compared to the life_expectancy() function.
#' Allows the user to add in the central death rate and proportion surviving to age x.
#' Allows the user to omit accessory columns which are used to calculate life expectancy.
#'
#'
#' @param data The mortality dataset, includes an age grouping variable,
#' @param age The age grouping variable, must be categorical
#' @param pop Population of each age group, must be numeric
#' @param deaths The midyear number of deaths at each age group, must be numeric
#' @param includeAllSteps If false, will only include the proportion surviving to age x and life expectancy for age x
#' @param includeCDR If true, will include the central death rate for each age group
#' @param includePS If true, will include the proportion surviving for each age group
#' @param ... Other optional grouping variables (can be race, gender, etc.)
#' @import dplyr
#' @return Lifetable
#' @export
#'
#' @examples
#' # Running lifetable() and choosing not to include CentralDeathRate and
#' # ProportionToSurvive (optional columns) in the output dataset
#'
#' lifetable(mortality2, "age_group", "population", "deaths", FALSE, TRUE, TRUE)
lifetable <- function(data, age, pop, deaths, includeAllSteps=TRUE, includeCDR=TRUE, includePS=TRUE, ...) {
data <- data %>%
group_by(...) %>%
central_death_rate(., age, pop, deaths) %>%
life_expectancy(., age, pop, deaths)
if (!includeCDR) {
data <- select(data, -CentralDeathRate)
}
if (!includePS) {
data <- select(data, -PropToSurvive)
}
if (!includeAllSteps) {
data <- select(data, -c(ConditionalProbDeath, ConditionalProbLife, NumberToSurvive, PersonYears, TotalYears))
}
return(data)
}