# Convert Dates to Year/Quarter Format in R (3 Examples)

This article illustrates how to change the format of dates to years and quarters in R.

Letâ€™s jump right to the programming part:

## Creation of Example Data

The following data is used as basement for this tutorial:

```my_dates <- as.Date(c("2020-05-10",                  # Create example dates
"2023-10-01",
"2022-08-07"))
my_dates                                             # Print example dates
# [1] "2020-05-10" "2023-10-01" "2022-08-07"```

As you can see based on the previous RStudio console output, the example data is a vector consisting of three date elements.

Currently, these dates are formatted as years-months-days. The following examples show how to convert our dates to a year-quarter format.

## Example 1: Convert Dates to Quarterly Format Using Base R

In this example, Iâ€™ll show how to use the basic installation of the R programming language to format our dates as years and quarters.

For this, we can use the paste, format, sprintf, and as.POSIXlt functions as shown below:

```my_dates_quarters1 <- paste(format(my_dates, "%Y"),  # Convert dates to quarterly
sprintf("%02i", (as.POSIXlt(my_dates)\$mon) %/% 3L + 1L),
sep = "/")
my_dates_quarters1                                   # Print quarterly dates
# [1] "2020/02" "2023/04" "2022/03"```

Have a look at the previous output of the RStudio console: As you can see, we have created a new data object that contains our three dates in year-quarterly format.

In this example, we have only used the functions of Base R. However, this lead to a relatively complicated R syntax.

Fortunately, the R programming language provides different packages that make the conversion of dates much simpler.

Letâ€™s do this!

## Example 2: Convert Dates to Quarterly Format Using lubridate Package

Example 2 demonstrates how to use the lubridate package to format our dates.

First, we have to install and load the lubridate package:

```install.packages("lubridate")                        # Install & load lubridate
library("lubridate")```

Next, we can use the year and quarter functions of the lubridate package to modify our dates:

```my_dates_quarters3 <- paste0(year(my_dates),         # Convert dates to quarterly
"/0",
quarter(my_dates))
my_dates_quarters3                                   # Print quarterly dates
# [1] "2020/02" "2023/04" "2022/03"```

As you can see, the output of the previous R syntax is exactly the same as in Example 1. However, this time the code was much less complicated.

## Example 3: Convert Dates to Quarterly Format Using zoo Package

Another package that provides functions for the manipulation of dates is the zoo package.

We first need to install and load the zoo package, to be able to use the corresponding functions:

```install.packages("zoo")                              # Install & load zoo
library("zoo")```

Next, we can apply the as.yearqtr function to format our dates as years and quarters:

```my_dates_quarters2 <- as.yearqtr(my_dates,           # Convert dates to quarterly
format = "%Y-%m-%d")
my_dates_quarters2                                   # Print quarterly dates
# [1] "2020 Q2" "2023 Q4" "2022 Q3"```

The previous output is a bit different compared to Examples 1 and 2, i.e. the quarters are represented by an uppercase Q.

## Video & Further Resources

Would you like to know more about dates in R? Then you might want to watch the following video of my YouTube channel. I explain the R code of this article in the video.

In addition to the video, you could read some of the other R programming tutorials of Statistics Globe. A selection of tutorials about date manipulation is listed below:

Summary: This post has demonstrated how to convert date objects to years and quarters in the R programming language. In case you have any further questions, please let me know in the comments section. Furthermore, donâ€™t forget to subscribe to my email newsletter to get updates on the newest tutorials.

Subscribe to the Statistics Globe Newsletter

• Hello Joachim,
Thank you so much for this video. This is very helpful. I have a further question. If there is a table with 3 columns (Items numbers, Values, and individual dates), and if we want to convert dates to Quarters-years to know which Item number has the maximum Value for a particular Quarter-Year then how do you write that code? Thank you in advance.
Regards,