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

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.

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.

Index functions

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

See also

Examples

# coerce POSIXct/Dates to yearweek, yearmonth, yearquarter x <- seq(as.Date("2016-01-01"), as.Date("2016-12-31"), by = "1 month") yearweek(x)
#> [1] "2015 W53" "2016 W05" "2016 W09" "2016 W13" "2016 W17" "2016 W22" #> [7] "2016 W26" "2016 W31" "2016 W35" "2016 W39" "2016 W44" "2016 W48"
yearmonth(x)
#> [1] "2016 Jan" "2016 Feb" "2016 Mar" "2016 Apr" "2016 May" "2016 Jun" #> [7] "2016 Jul" "2016 Aug" "2016 Sep" "2016 Oct" "2016 Nov" "2016 Dec"
yearmonth(yearweek(x))
#> [1] "2015 Dec" "2016 Feb" "2016 Feb" "2016 Mar" "2016 Apr" "2016 May" #> [7] "2016 Jun" "2016 Aug" "2016 Aug" "2016 Sep" "2016 Oct" "2016 Nov"
yearmonth("2018-07")
#> [1] "2018 Jul"
yearquarter(x)
#> [1] "2016 Q1" "2016 Q1" "2016 Q1" "2016 Q2" "2016 Q2" "2016 Q2" "2016 Q3" #> [8] "2016 Q3" "2016 Q3" "2016 Q4" "2016 Q4" "2016 Q4"
# coerce yearmonths to yearquarter y <- yearmonth(x) yearquarter(y)
#> [1] "2016 Q1" "2016 Q1" "2016 Q1" "2016 Q2" "2016 Q2" "2016 Q2" "2016 Q3" #> [8] "2016 Q3" "2016 Q3" "2016 Q4" "2016 Q4" "2016 Q4"
# seq() and binary operaters wk1 <- yearweek("2017-11-01") wk2 <- yearweek("2018-04-29") seq(from = wk1, to = wk2, by = 2) # by two weeks
#> [1] "2017 W44" "2017 W46" "2017 W48" "2017 W50" "2017 W52" "2018 W02" #> [7] "2018 W04" "2018 W06" "2018 W08" "2018 W10" "2018 W12" "2018 W14" #> [13] "2018 W16"
wk1 + 0:9
#> [1] "2017 W44" "2017 W45" "2017 W46" "2017 W47" "2017 W48" "2017 W49" #> [7] "2017 W50" "2017 W51" "2017 W52" "2018 W01"
mth <- yearmonth("2017-11-01") seq(mth, length.out = 5, by = 1) # by 1 month
#> [1] "2017 Nov" "2017 Dec" "2018 Jan" "2018 Feb" "2018 Mar"
mth + 0:9
#> [1] "2017 Nov" "2017 Dec" "2018 Jan" "2018 Feb" "2018 Mar" "2018 Apr" #> [7] "2018 May" "2018 Jun" "2018 Jul" "2018 Aug"
seq(yearquarter(mth), length.out = 5, by = 1) # by 1 quarter
#> [1] "2017 Q4" "2018 Q1" "2018 Q2" "2018 Q3" "2018 Q4"
# different formats format(c(wk1, wk2), format = "%V/%Y")
#> [1] "44/2017" "17/2018"
format(y, format = "%y %m")
#> [1] "16 01" "16 02" "16 03" "16 04" "16 05" "16 06" "16 07" "16 08" "16 09" #> [10] "16 10" "16 11" "16 12"
format(yearquarter(mth), format = "%y Qtr%q")
#> [1] "17 Qtr4"
is_53weeks(2015:2016)
#> [1] TRUE FALSE