datesToAgeGroups: Calculate ages, periods, cohorts, and Lexis triangles from...

Description Usage Arguments Details Value See Also Examples

View source: R/miscellaneous-functions.R

Description

Calculates ages in completed years, periods, birth cohorts, or Lexis triangles from dates.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
datesToAgeGroups(date, dob, step = "1 year", lastOpen = TRUE)

datesToCohorts(dob, step = "years", monthStart = "January", yearStart = 2000)

datesToPeriods(date, step = "years", monthStart = "January", yearStart = 2000)

datesToTriangles(
  date,
  dob,
  step = "years",
  monthStart = "January",
  yearStart = 2000
)

Arguments

date

A vector of class Date. All elements of date must be equal to or greater than the corresponding elements of dob.

dob

A vector of class Date.

step

Length of age-time step.

lastOpen

Whether last age interval is open on the right.

monthStart

The English name of a month (as defined by month.name. Can be abbreviated.

yearStart

An integer. Controls the start date of multi-year periods.

Details

If date and dob are different lengths, the shorter vector is recycled.

By default, age-time steps are assumed to be equal to one year. However, alternative lengths can be specified, via the step argument. The steps are described as multiples of years, quarters or months. If no multiple is provided, it is assumed to equal one. Typical values are "5 years", "month", "quarter", and "6 months". If a step length is less than 1 year, then it must divide the year without leaving a remainder. Thus, for example, "4 months" is a valid value for step, but "5 months" is not.

By default, periods of one year are assumed to start on 1 January. Other starting months can be specified via monthStart. See below for examples.

Multi-year periods are assumed to start on years beginning with 0 or 5. Other values can be specified via yearStart. See below for examples.

These functions handle leap years differently from seq.Date. Like most people, but unlike seq.Date, they treat 29 February as if it was 28 February. For instance, with datesToAge, a person who was born on 29 February 2000 reaches age 1 on 28 February 2001. With seq.Date, they reach age 1 on 1 March 2001.

Value

A factor.

See Also

Vectors of class Date can be created with function as.Date.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
date <- as.Date(c("2005-07-05", "2006-06-30", "2008-07-05"))
dob <- as.Date(c("2005-06-30", "2005-06-30", "2006-07-01"))
datesToAgeGroups(date = date, dob = dob)
datesToAgeGroups(date = date, dob = dob, lastOpen = FALSE)
datesToAgeGroups(date = date, dob = dob, step = "2 years")
datesToAgeGroups(date = date, dob = dob, step = "quarter")
datesToAgeGroups(date = date, dob = dob, step = "month")
datesToPeriods(date = date)
datesToPeriods(date = date, monthStart = "July")
datesToPeriods(date = date, step = "2 years", yearStart = 2001)
datesToPeriods(date = date, step = "quarter")
datesToCohorts(dob)
datesToCohorts(dob, step = "2 quarters")
datesToTriangles(date = date, dob = dob)
datesToTriangles(date = date, dob = dob, step = "2 years")
datesToTriangles(date = date, dob = dob, monthStart = "April")

## 'date' must be later than 'dob'
## Not run: 
datesToAgeGroups(date = as.Date("2000-01-01"), dob = as.Date("2005-01-01"))

## End(Not run)

StatisticsNZ/dembase documentation built on Dec. 25, 2021, 4:49 p.m.