- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
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
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"
- Related Articles
- How to convert year, month, and day of the month into a complete date in R?
- How to convert a date or date vector to POSIXct in R?
- Format MySQL date and convert to year-month-day
- How to get a Date from year, month and day in Java?
- How to convert a MySQL date to JavaScript date?
- How to convert a data frame column to date that contains integer values in R?
- How to convert a Python date string to a date object?
- How to convert a string to date in MySQL?
- Format date in MySQL to return MonthName and Year?
- How to convert a date format in MySQL?
- How to add a year and two days to a date with a single MySQL query?
- How do I discover the Quarter of a given Date in Java?
- How to convert epoch Date to meaningful JavaScript date?
- How to correctly convert a date format into a MySQL date?
- How to convert a Date value to string in JDBC?

Advertisements