Zeno Shuai
Zeno Shuai

Reputation: 109

customize the display format of tsibble yearmonth column?

Can I customize the display format of the time series column in tsibble ?

yearmonth allways like %Y %m,but i would like change the default format to %m-%y all over one chunk

tsibble::format not working

Upvotes: -1

Views: 118

Answers (1)

joshbrows
joshbrows

Reputation: 813

Maybe you're looking for format('%b-%Y')?

df <- data.frame(
  date = c("2022-01-01","2022-02-01","2022-03-01",
           "2022-04-01","2022-05-01","2022-06-01","2022-07-01",
           "2022-08-01","2022-09-01","2022-10-01","2022-11-01","2022-12-01")
)

df |>
  dplyr::mutate(
    date = tsibble::yearmonth(date) |>
      format('%b-%Y')
  )
#>        date
#> 1  Jan-2022
#> 2  Feb-2022
#> 3  Mar-2022
#> 4  Apr-2022
#> 5  May-2022
#> 6  Jun-2022
#> 7  Jul-2022
#> 8  Aug-2022
#> 9  Sep-2022
#> 10 Oct-2022
#> 11 Nov-2022
#> 12 Dec-2022

Another option is to use zoo::yearmon().

df |>
  dplyr::mutate(
    date = zoo::as.yearmon(date)
  )
#>        date
#> 1  Jan 2022
#> 2  Feb 2022
#> 3  Mar 2022
#> 4  Apr 2022
#> 5  May 2022
#> 6  Jun 2022
#> 7  Jul 2022
#> 8  Aug 2022
#> 9  Sep 2022
#> 10 Oct 2022
#> 11 Nov 2022
#> 12 Dec 2022

Upvotes: 1

Related Questions