Format date as Year/Quarter in R - GeeksforGeeks

23 May, 2021
A date can be represented in multiple formats as per requirement. Some requirements only want information regarding year and quarter and other parts of a date seem unnecessary in such scenarios. In such cases, the date can be formatted to display only year and quarter. In this article we, will be looking at the different approaches to format date as year/quarter in R language.
paste() function is used to Concatenate vectors after converting to character.
Syntax: paste (..., sep = ” “, collapse = NULL)
format() function helps to format an R object for pretty printing.
Syntax: format(x, ...)
sprintf() function basically returns a character vector containing a formatted combination of text and variable values.
Syntax: sprintf(fmt, ...)
as.POSIXLt() function is used to manipulate objects of classes representing calendar dates and times.
Syntax: as.POSIXlt(x, tz = “”, ...)
Example:
R
gfg_date=as.Date(c("2021-05-13","2000-07-12",                   "2020-10-08","2001-09-04"))  gfg_date  gfg_quarters <- paste(  format(gfg_date, "%Y"),  sprintf("%02i", (as.POSIXlt(gfg_date)$mon) %/% 3L + 1L),   sep = "/")  gfg_quarters
Output:
In this approach to format given dates as quarter user needs to call paste0() function and quarter() function of the lubridate package and pass the required parameters into it. Then this approach will be converting the given date as year/quarters in R programming language.
paste0() function is used to concatenate vectors after converting to character.
Syntax: paste0(..., collapse = NULL)
quarter() function is used to divide the year into fourths.
Syntax:
quarter(x, with_year = FALSE, fiscal_start = 1)
Parameters:
x:-a date-time object of class POSIXct, POSIXlt, Date, chron, yearmon, yearqtr, zoo, zooreg, timeDate, xts, its, ti, jul, timeSeries, fts or anything else that can be converted with as.POSIXlt
with_year:-logical indicating whether or not to include the quarter’s year.
fiscal_start:-numeric indicating the starting month of a fiscal year
Example:
R
library("lubridate")  gfg_date=as.Date(c("2021-05-13","2000-07-12",                   "2020-10-08","2001-09-04"))gfg_date  gfg_quarters <- paste0(year(gfg_date),"/0",quarter(gfg_date))gfg_quarters
Output:
Under this approach to format given dates as year/quarter users need to first install and load the zoo package. Then call the as.yearqtr() function with the required parameters into it, with this user will be getting a formatted date as year/quarter in return.
as.yearqtr() function is used for representing quarterly data.
Syntax: as.yearqtr(x, format, ...)
Parameter:
x:-for yearqtr a numeric (interpreted as being “in years”). For as.yearqtr another date class object.
format:-character string specifying format. For coercing to “yearqtr” from character: “%Y” and “%q” have to be specified.
...:-arguments passed ot other methods.
Example:
R
library("zoo")  gfg_date=as.Date(c("2021-05-13","2000-07-12",                   "2020-10-08","2001-09-04"))gfg_date  gfg_quarters <-as.yearqtr(gfg_date,format = "%Y-%m-%d")gfg_quarters
Output:
Picked
R-DateTime
R Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Control Statements in R Programming
Change Color of Bars in Barchart using ggplot2 in R
Data Visualization in R
Group by function in R using Dplyr
How to Change Axis Scales in R Plots?
Logistic Regression in R Programming
Linear Discriminant Analysis in R Programming
How to filter R DataFrame by values in a column?
How to change Colors in ggplot2 Line Plot in R ?
How to import an Excel File into R ?