tsibble (version 0.5.2)

yearweek: Represent year-week (ISO), year-month or year-quarter objects

Description

Create or coerce using yearweek(), yearmonth(), or yearquarter()

Usage

yearweek(x)

is_53weeks(year)

yearmonth(x)

yearquarter(x)

Arguments

x

Other object.

year

A vector of years.

Value

Year-week (yearweek), year-month (yearmonth) or year-quarter (yearquarter) objects.

TRUE/FALSE if the year has 53 ISO weeks.

Index functions

The tsibble yearmonth() and yearquarter() function preserve the time zone of the input x, contrasting to their zoo counterparts.

Details

It's a known issue that these attributes will be dropped when using group_by and mutate together. It is recommended to ungroup first, and then use mutate.

See Also

pull_interval

Examples

Run this code
# NOT RUN {
# coerce POSIXct/Dates to yearweek, yearmonth, yearquarter ----
x <- seq(as.Date("2016-01-01"), as.Date("2016-12-31"), by = "1 month")
yearweek(x)
yearmonth(yearweek(x)); yearmonth(x)
yearmonth("201807")
yearquarter(x)

# coerce numerics to yearmonth, yearquarter ----
yearmonth(seq(2010, 2017, by = 1 / 12))
yearquarter(seq(2010, 2017, by = 1 / 4))

# coerce yearmonths to yearquarter ----
y <- yearmonth(x)
yearquarter(y)

# S3 method seq() ----
wk1 <- yearweek(as.Date("2017-11-01"))
wk2 <- yearweek(as.Date("2018-04-29"))
seq(from = wk1, to = wk2, by = 2) # by two weeks
mth <- yearmonth(as.Date("2017-11-01"))
seq(mth, length.out = 5, by = 1) # by 1 month
seq(yearquarter(mth), length.out = 5, by = 1) # by 1 quarter
is_53weeks(2015:2016)
# }

Run the code above in your browser using DataLab