How to convert a date to quarter and year in R?


Most of the times the date data is available only in date format and there is not information about the quarter of the year. The quarter of the year is required when we want compare the quarters or do some time series analysis. For the conversion of date into quarter and year can be done by using as.yearqtr function from zoo package as shown in the below examples.

Example

library(zoo)
as.yearqtr("2021-01-19",format="%Y-%m-%d")

Output

[1] "2021 Q1"

as.yearqtr("2021-04-19",format="%Y-%m-%d")

[1] "2021 Q2"

as.yearqtr(c("2021-04-19","2020-05-24","2020-11-09"),format="%Y-%m-%d")

[1] "2021 Q2" "2020 Q2" "2020 Q4"

Example

 Live Demo

x1<-sample(c("2021-04-19","2020-05-24","2020-11-09"),50,replace=TRUE)
x1

Output

[1]  "2020-05-24" "2020-05-24" "2021-04-19" "2021-04-19" "2021-04-19"
[6]  "2021-04-19" "2021-04-19" "2020-11-09" "2020-11-09" "2020-11-09"
[11] "2020-05-24" "2021-04-19" "2020-05-24" "2020-05-24" "2020-05-24"
[16] "2020-11-09" "2020-05-24" "2020-05-24" "2020-11-09" "2021-04-19"
[21] "2020-11-09" "2021-04-19" "2020-05-24" "2021-04-19" "2020-05-24"
[26] "2020-05-24" "2020-05-24" "2020-11-09" "2020-05-24" "2020-05-24"
[31] "2020-05-24" "2020-11-09" "2020-05-24" "2020-05-24" "2021-04-19"
[36] "2021-04-19" "2020-05-24" "2020-11-09" "2021-04-19" "2020-05-24"
[41] "2021-04-19" "2020-11-09" "2021-04-19" "2020-11-09" "2020-11-09"
[46] "2020-11-09" "2020-05-24" "2020-11-09" "2021-04-19" "2020-05-24"

as.yearqtr(x1,format="%Y-%m-%d")

[1]  "2020 Q2" "2020 Q2" "2021 Q2" "2021 Q2" "2021 Q2" "2021 Q2" "2021 Q2"
[8]  "2020 Q4" "2020 Q4" "2020 Q4" "2020 Q2" "2021 Q2" "2020 Q2" "2020 Q2"
[15] "2020 Q2" "2020 Q4" "2020 Q2" "2020 Q2" "2020 Q4" "2021 Q2" "2020 Q4"
[22] "2021 Q2" "2020 Q2" "2021 Q2" "2020 Q2" "2020 Q2" "2020 Q2" "2020 Q4"
[29] "2020 Q2" "2020 Q2" "2020 Q2" "2020 Q4" "2020 Q2" "2020 Q2" "2021 Q2"
[36] "2021 Q2" "2020 Q2" "2020 Q4" "2021 Q2" "2020 Q2" "2021 Q2" "2020 Q4"
[43] "2021 Q2" "2020 Q4" "2020 Q4" "2020 Q4" "2020 Q2" "2020 Q4" "2021 Q2"
[50] "2020 Q2"

Example

 Live Demo

x2<-sample(c("2020-02-05","2020-08-24","2020-06-17","2021-04-19","2020-05-24","2020-11-09"),80,replace=TRUE)
x2

Output

[1]  "2020-02-05" "2020-08-24" "2020-05-24" "2020-11-09" "2020-02-05"
[6]  "2020-02-05" "2020-08-24" "2020-11-09" "2020-11-09" "2020-05-24"
[11] "2020-02-05" "2020-06-17" "2021-04-19" "2020-06-17" "2020-08-24"
[16] "2020-06-17" "2020-08-24" "2021-04-19" "2020-06-17" "2020-08-24"
[21] "2020-06-17" "2020-05-24" "2020-02-05" "2020-06-17" "2020-05-24"
[26] "2020-05-24" "2020-06-17" "2020-02-05" "2020-11-09" "2020-02-05"
[31] "2021-04-19" "2020-05-24" "2020-06-17" "2020-02-05" "2020-02-05"
[36] "2020-11-09" "2020-05-24" "2020-06-17" "2020-06-17" "2020-06-17"
[41] "2020-11-09" "2020-08-24" "2020-08-24" "2020-06-17" "2021-04-19"
[46] "2020-06-17" "2020-11-09" "2020-08-24" "2020-05-24" "2020-08-24"
[51] "2020-02-05" "2020-11-09" "2020-11-09" "2020-06-17" "2020-11-09"
[56] "2020-02-05" "2020-11-09" "2020-06-17" "2020-05-24" "2021-04-19"
[61] "2020-11-09" "2021-04-19" "2020-02-05" "2020-06-17" "2020-06-17"
[66] "2021-04-19" "2021-04-19" "2021-04-19" "2020-11-09" "2020-06-17"
[71] "2020-11-09" "2021-04-19" "2021-04-19" "2020-02-05" "2020-05-24"
[76] "2020-02-05" "2020-02-05" "2020-11-09" "2020-02-05" "2020-06-17"

as.yearqtr(x2,format="%Y-%m-%d")

[1] "2020 Q1" "2020 Q3" "2020 Q2" "2020 Q4" "2020 Q1" "2020 Q1" "2020 Q3"
[8] "2020 Q4" "2020 Q4" "2020 Q2" "2020 Q1" "2020 Q2" "2021 Q2" "2020 Q2"
[15] "2020 Q3" "2020 Q2" "2020 Q3" "2021 Q2" "2020 Q2" "2020 Q3" "2020 Q2"
[22] "2020 Q2" "2020 Q1" "2020 Q2" "2020 Q2" "2020 Q2" "2020 Q2" "2020 Q1"
[29] "2020 Q4" "2020 Q1" "2021 Q2" "2020 Q2" "2020 Q2" "2020 Q1" "2020 Q1"
[36] "2020 Q4" "2020 Q2" "2020 Q2" "2020 Q2" "2020 Q2" "2020 Q4" "2020 Q3"
[43] "2020 Q3" "2020 Q2" "2021 Q2" "2020 Q2" "2020 Q4" "2020 Q3" "2020 Q2"
[50] "2020 Q3" "2020 Q1" "2020 Q4" "2020 Q4" "2020 Q2" "2020 Q4" "2020 Q1"
[57] "2020 Q4" "2020 Q2" "2020 Q2" "2021 Q2" "2020 Q4" "2021 Q2" "2020 Q1"
[64] "2020 Q2" "2020 Q2" "2021 Q2" "2021 Q2" "2021 Q2" "2020 Q4" "2020 Q2"
[71] "2020 Q4" "2021 Q2" "2021 Q2" "2020 Q1" "2020 Q2" "2020 Q1" "2020 Q1"
[78] "2020 Q4" "2020 Q1" "2020 Q2"

Updated on: 06-Feb-2021

649 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements