Skip to contents

This function takes a dataset with a specified date of birth column and returns the dataset with an additional column which contains the calculated age.

Usage

construct_age(dataset, dob_col, age_col = "birth3b", end_date_col = NULL)

Arguments

dataset

A data frame containing at least one column with date of birth values.

dob_col

The name of the column containing date of birth values.

age_col

The name of the new column to store the calculated age. Default is `"age"`.

end_date_col

Optional. The name of the column containing end date values. If not provided, today's date is used.

Value

A data frame with an additional column containing the calculated age.

Examples

df <- data.frame(death2a = as.Date(c("1990-05-15", "1985-10-30", "2000-07-22")),
                 death1a = as.Date(c("2022-07-15", "2022-07-15", "2022-07-15")))
new_df <- construct_age(df, dob_col = "death2a", age_col = "death2b", end_date_col = "death1a")
print(new_df)
#>      death2a    death1a death2b
#> 1 1990-05-15 2022-07-15      32
#> 2 1985-10-30 2022-07-15      36
#> 3 2000-07-22 2022-07-15      21

df <- data.frame(birth3a = as.Date(c("1990-05-15", "1985-10-30", "2000-07-22")))
new_df <- construct_age(df, dob_col = "birth3a")
print(new_df)
#>      birth3a birth3b
#> 1 1990-05-15      34
#> 2 1985-10-30      38
#> 3 2000-07-22      24