Quantcast
Channel: Format date as Year/Quarter - Stack Overflow
Browsing all 11 articles
Browse latest View live

Answer by Maël for Format date as Year/Quarter

tidyverse's clock gives an alternative, with the advantage of not loosing the original day, with calendar_narrow and date precision:library(clock)library(dplyr)#Function to convert a date to...

View Article



Answer by Rene Chan for Format date as Year/Quarter

In case someone is looking for a format such as 1Q21 for 1st quarter 21, I used @Roland answer above and made a small change towards:paste(sprintf("%2i", (as.POSIXlt(Notional$Date)$mon) %/% 3L + 1L),...

View Article

Answer by andschar for Format date as Year/Quarter

The data.table package and its IDate class have some nice convenience functions (e.g. quarter(), year()), similar to those of lubridate available. paste0() them together as you please.Data <-...

View Article

Answer by AlexB for Format date as Year/Quarter

another option would be:Data$qtr <- lubridate::quarter(Data$date, with_year = T)

View Article

Answer by Jeff Parker for Format date as Year/Quarter

I have been loving the lubridate package for working with dates. Super slick. The quarter function finds the quarter (of course) and then just pair that with the year.library(lubridate)Data <- Data...

View Article


Answer by Beginning_Math for Format date as Year/Quarter

I made a similar format using quarters() and sub() in R:Data$qtr <- paste(format(Data$date, "%y/"), 0, sub( "Q", "", quarters(Data$date) ), sep = "")

View Article

Answer by yonicd for Format date as Year/Quarter

yq=function(x,prefix="%Y",combine="Q") paste0(ifelse(is.null(prefix),"",format(x,"%Y")),floor(as.numeric(format(x,"%m"))/3-1e-3)+1,sep=combine)this gives the flexibility of returning any format back...

View Article

Answer by Dominic for Format date as Year/Quarter

Another (longer) way of doing it using if statements is this:month <- as.numeric(format(date, format = "%m"))[1]if (month < 4) { quarter <- paste( format(date, format = "%Y")[1], "Q1",...

View Article


Answer by Roland for Format date as Year/Quarter

Using base functions:Data$date <- as.Date(Data$date)Data$qtr <- paste(format(Data$date, "%y"), sprintf("%02i", (as.POSIXlt(Data$date)$mon) %/% 3L + 1L), sep="/")# date qtr# 1 2001-01-01 01/01# 2...

View Article


Answer by tonytonov for Format date as Year/Quarter

You need to explicilty Vectorize your function: fun_v <- Vectorize(fun, "x")fun_v(Data$date)#[1] "01/01""01/01""01/01""01/02""01/02""01/02"However, when it comes to more or less standard tasks (such...

View Article

Format date as Year/Quarter

I have the following dataframe:Data <- data.frame( date = c("2001-01-01", "2001-02-01", "2001-03-01", "2001-04-01", "2001-05-01", "2001-06-01"), qtr = c("NA", "NA","NA","NA","NA","NA"))I want to...

View Article
Browsing all 11 articles
Browse latest View live


Latest Images