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 preserve the time zone 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(yearweek(x)); yearmonth(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"
#> [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"
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 numerics to yearmonth, yearquarter ---- yearmonth(seq(2010, 2017, by = 1 / 12))
#> [1] "2010 Jan" "2010 Feb" "2010 Mar" "2010 Apr" "2010 May" "2010 Jun" #> [7] "2010 Jul" "2010 Aug" "2010 Sep" "2010 Oct" "2010 Nov" "2010 Dec" #> [13] "2011 Jan" "2011 Feb" "2011 Mar" "2011 Apr" "2011 May" "2011 Jun" #> [19] "2011 Jul" "2011 Aug" "2011 Sep" "2011 Oct" "2011 Nov" "2011 Dec" #> [25] "2012 Jan" "2012 Feb" "2012 Mar" "2012 Apr" "2012 May" "2012 Jun" #> [31] "2012 Jul" "2012 Aug" "2012 Sep" "2012 Oct" "2012 Nov" "2012 Dec" #> [37] "2013 Jan" "2013 Feb" "2013 Mar" "2013 Apr" "2013 May" "2013 Jun" #> [43] "2013 Jul" "2013 Aug" "2013 Sep" "2013 Oct" "2013 Nov" "2013 Dec" #> [49] "2014 Jan" "2014 Feb" "2014 Mar" "2014 Apr" "2014 May" "2014 Jun" #> [55] "2014 Jul" "2014 Aug" "2014 Sep" "2014 Oct" "2014 Nov" "2014 Dec" #> [61] "2015 Jan" "2015 Feb" "2015 Mar" "2015 Apr" "2015 May" "2015 Jun" #> [67] "2015 Jul" "2015 Aug" "2015 Sep" "2015 Oct" "2015 Nov" "2015 Dec" #> [73] "2016 Jan" "2016 Feb" "2016 Mar" "2016 Apr" "2016 May" "2016 Jun" #> [79] "2016 Jul" "2016 Aug" "2016 Sep" "2016 Oct" "2016 Nov" "2016 Dec" #> [85] "2017 Jan"
yearquarter(seq(2010, 2017, by = 1 / 4))
#> [1] "2010 Q1" "2010 Q2" "2010 Q3" "2010 Q4" "2011 Q1" "2011 Q2" "2011 Q3" #> [8] "2011 Q4" "2012 Q1" "2012 Q2" "2012 Q3" "2012 Q4" "2013 Q1" "2013 Q2" #> [15] "2013 Q3" "2013 Q4" "2014 Q1" "2014 Q2" "2014 Q3" "2014 Q4" "2015 Q1" #> [22] "2015 Q2" "2015 Q3" "2015 Q4" "2016 Q1" "2016 Q2" "2016 Q3" "2016 Q4" #> [29] "2017 Q1"
# 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"
# 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
#> [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"
mth <- yearmonth(as.Date("2017-11-01")) seq(mth, length.out = 5, by = 1) # by 1 month
#> [1] "2017 Nov" "2017 Dec" "2018 Jan" "2018 Feb" "2018 Mar"
seq(yearquarter(mth), length.out = 5, by = 1) # by 1 quarter
#> [1] "2017 Q4" "2018 Q1" "2018 Q2" "2018 Q3" "2018 Q4"
is_53weeks(2015:2016)
#> [1] TRUE FALSE