italianfoot
italianfoot

Reputation: 199

Convert YYYYQ using as.yearqtr

I have a char field with the format YYYYQ (e.g., 20124, 20131, etc) and I want to convert it to a field format by using as.yearqtr from the zoo package.

Thanks for your help!

Upvotes: 0

Views: 10312

Answers (1)

G. Grothendieck
G. Grothendieck

Reputation: 269644

1) Be sure to convert to character first:

library(zoo)

x <- c(20124, 20131)

as.yearqtr(format(x), "%Y%q")
## [1] "2012 Q4" "2013 Q1"

2) This would also work. It converts the number to year + fraction of a year:

as.yearqtr(x %/% 10 + ((x %% 10) - 1)/4)
## [1] "2012 Q4" "2013 Q1"

3) as would this which converts the number to yyyy-0q first:

as.yearqtr(sub("(.)$", "-0\\1", x))
## [1] "2012 Q4" "2013 Q1"

Upvotes: 7

Related Questions