timeToPeriod: Convert years to periods

Description Usage Arguments Details Value See Also Examples

View source: R/miscellaneous-functions.R

Description

Convert a vector of years to a vector of periods. The periods are formatted in the way expected by functions such as Counts and Values.

Usage

1
timeToPeriod(year, breaks, firstOpen = FALSE, lastOpen = FALSE)

Arguments

year

A vector of years. A numeric vector, or a vector than can be coerced to numeric.

breaks

A vector of breaks, specifying the points dividing periods.

firstOpen

Logical. Whether the first period is "open", i.e. has no lower bound. Defaults to FALSE.

lastOpen

Logical. Whether the last period is "open", i.e. has no upper bound. Defaults to FALSE.

Details

The years can be contain decimal fractions such as 2000.25 or 2018.633, but are more typically integers such as 2000 or 2018.

If year is a factor, then timeToPeriod will coerce it to a character vector before trying to coerce it to numeric. See below for an example.

Value

A factor, the same length as year.

See Also

ageToAgeGroup

Examples

1
2
3
4
5
6
7
year <- c(2001, 2023, 2000, 2005, 2014, 2013, 2029)
## 5-year periods, 2000-2005, 2005-2010, ..., 2025-2030
timeToPeriod(year, breaks = seq(2000, 2030, 5))
## 5-year periods, 2000-2005, 2005-2010, ..., 2045-2050
timeToPeriod(year, breaks = seq(2000, 2050, 5))
## first period open 
timeToPeriod(year, breaks = seq(2010, 2030, 5), firstOpen = TRUE)

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