date_to_period_year: Convert dates to one-year periods

Description Usage Arguments Details Value See Also Examples

View source: R/date_to_period.R

Description

Assign dates to one-year periods. The one-year periods are, by default, calendar years.

Usage

1
date_to_period_year(date, month_start = "Jan", label_year_start = TRUE)

Arguments

date

Dates of events or measurements. A vector of class Date, or a vector than can be coerced to class Date via function as.Date.

month_start

An element of month.name, or month.abb. Each period starts on the first day of this month.

label_year_start

Whether to label a period by the calendar year at the beginning of the period or the calendar year at the end. Defaults to TRUE.

Details

Periods start on the first day of month_start, and end one-year-minus-one-day later. The default value for month_start is "Jan", so periods by default start on 1 January and end on 31 December. month_start can be a full month name or an abbreviation.

If a period starts on 1 January, then the first day and last day of the period belong to the same calendar year. But if a period starts on any other day, then the first day belongs to one calendar year and the last day belongs to the next calendar year. For instance, if a period extends from 1 July 2000 to 30 June 2001, then the first day belongs to calendar year 2000, and the last day belongs to calendar year 2001. Some people use the first year to label such periods, and others use the last year. For instance, if a period extends from 1 July 2000 to 30 June 2001, some people label the period "2000", and others label it "2001". Function date_to_period_year by default uses the start year. To use the end year, set label_year_start to FALSE.

Value

An integer vector with the same length as date.

See Also

The output from date_to_period_year is often processed further using format_period_year.

Other functions for creating periods from dates are date_to_period_quarter and and date_to_period_month.

Other functions for creating one-year units from dates are date_to_age_year, date_to_cohort_year, and date_to_triangle_year. The interface for date_to_period_year is identical to that of date_to_cohort_year.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
date_to_period_year(date = c("2024-03-27", "2022-11-09"))

## period starts on 1 July, not 1 January
date_to_period_year(date = c("2024-03-27", "2022-11-09"),
                    month_start = "Jul")

## period starts on 1 July, using the calendar year at
## the end, rather than beginning, for the label
date_to_period_year(date = c("2024-03-27", "2022-11-09"),
                    month_start = "Jul",
                    label_year_start = FALSE)

johnrbryant/demprep documentation built on Dec. 31, 2021, 11:58 a.m.