emehex
emehex

Reputation: 10568

Rearranging the zoo::as.yearqtr() output

I know that I can call:

library(zoo)
Sys.Date()
CQ <- as.yearqtr(Sys.Date())
CQ

And get:

"2015-03-05"
"2015 Q1"

But I need my output to be:

"Q1-2015"

How can I convert CQ to be QX-YYYY?

Upvotes: 1

Views: 291

Answers (2)

David Arenburg
David Arenburg

Reputation: 92300

You can achieve a similar result using the base R. For example,

x <- seq(Sys.Date(), Sys.Date() + 180L, by = "month") # Generate some dates vector
paste(quarters(x), as.POSIXlt(x)$year + 1900L, sep = "-")
## [1] "Q1-2015" "Q2-2015" "Q2-2015" "Q2-2015" "Q3-2015" "Q3-2015"

Upvotes: 1

akrun
akrun

Reputation: 887851

You could try

format(CQ, 'Q%q-%Y')
#[1] "Q1-2015"

Upvotes: 3

Related Questions