Skip to contents

A helper function primarily used by table_one() to derive the age of the person at enrollment but may have additional uses to derive age at an index event or date (in years). See details and examples.

Usage

get_person_age(
  ds,
  index_type = "event",
  index_event = "enrollment",
  index_date = NULL
)

Arguments

ds

A PicnicHealth data set list object.

index_type

The type of index against which to calculate age. Must be in c('event', 'date'), and defaults to 'event'.

index_event

What event to use to calculate age. Only used if index_type = 'event'. Currently only 'enrollment' is available, the default.

index_date

A vector of dates used to calculate age. Only used if index_type = "date". Defaults to NULL.

Value

A data frame, one row per person, with a column for person_id and age, i.e. person's age in years.

Details

If called with no other arguments than the PicnicHealth data set, i.e. ds, then the default return value is age at enrollment.

If specifying index_date, the number of elements (rows) in the vector of index_date must be equal to the number of rows in the person table because age is calculated using the date_of_birth column in that table.

Beyond its use in table_one(), this function may be useful to add an additional column to an existing analytic data.frame that already contains person_id using dplyr::left_join() to include age. See examples.

Examples

if (FALSE) { # \dontrun{
# Get age at enrollment:
get_person_age(ds = ds)

# Get age at death:
get_person_age(ds = ds, index_type = 'date', index_date = as.Date(ds$person$death_date))

# Add `age` to an analytic data set that contains `person_id`:
analytic_dataset %>%
  left_join(get_person_age(ds))
} # }