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

yearmonth(x)

yearquarter(x)

Arguments

x

Other object.

Value

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

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

pull_interval

Examples

# coerce dates to yearmonth, yearquarter ---- x <- seq(as.Date("2016-01-01"), as.Date("2016-12-31"), by = "1 month") 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"
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"