Trying to generate a function that determines the previous 3 Relevant Quarterly dates given the Current Day. Basically I am checking what quarter I am in then grabbing the three previous Quarters which can get complicated if it is currently Q2 and I need Q3 2021, Q4 2021, and Q1 2022. Why does my function currently return NA for all values I am confused! Please give me suggestions or solutions thanks you!
# set dates
first.date <- Sys.Date() - 60
last.date <- Sys.Date()
current.year <- format(last.date, format ="%Y")
quarters <- quarter(first.date)
prev.year <- as.numeric(current.year) - 1
quart.df <- data.frame(month = c(3, 6, 9, 12),
day = c(31, 30, 30, 31))
getQs <- function(date.curr){
current.year <- format(date.curr, format ="%Y")
prev.year <- as.numeric(current.year) - 1
prev.year <- str(prev.year)
q <- quarter(date.curr)
day <-
rel.qs <- c()
if (q == 1){
rel.qs[1] <- as.Date(with(quart.df, paste(prev.year,month[2],day[2])), "%Y-%m-%d")
rel.qs[2] <- as.Date(with(quart.df, paste(prev.year,month[3],day[3])), "%Y-%m-%d")
rel.qs[3] <- as.Date(with(quart.df, paste(prev.year,month[4],day[4])), "%Y-%m-%d")
}
if (q == 2){
rel.qs[1] <- as.Date(with(quart.df, paste(prev.year,month[3],day[3])), "%Y-%m-%d")
rel.qs[2] <- as.Date(with(quart.df, paste(prev.year,month[4],day[4])), "%Y-%m-%d")
rel.qs[3] <- as.Date(with(quart.df, paste(current.year,month[1],day[1])), "%Y-%m-%d")
}
if (q == 3){
rel.qs[1] <- as.Date(with(quart.df, paste(prev.year,month[4],day[4])), "%Y-%m-%d")
rel.qs[2] <- as.Date(with(quart.df, paste(current.year,month[1],day[1])), "%Y-%m-%d")
rel.qs[3] <- as.Date(with(quart.df, paste(current.year,month[2],day[2])), "%Y-%m-%d")
}
else{
rel.qs[1] <- as.Date(with(quart.df, paste(current.year,month[1],day[1])), "%Y-%m-%d")
rel.qs[2] <- as.Date(with(quart.df, paste(current.year,month[2],day[2])), "%Y-%m-%d")
rel.qs[3] <- as.Date(with(quart.df, paste(current.year,month[3],day[3])), "%Y-%m-%d")
}
return(rel.qs)
}
getQs(last.date)
CodePudding user response:
I would solve this problem by using the function ceiling_date() from the lubridate package. This would be my suggestion:
CurrentDate <- Sys.Date()
getQs <- function(StartDate, QuartersBack){
vec.Dates <- vector(length = QuartersBack)
class(vec.Dates) <- "Date"
for (i in c(1:QuartersBack)) {
vec.Dates[i] <- lubridate::ceiling_date(StartDate %m-% months(3*i), "quarter") - 1
}
vec.Dates
}
getQs(StartDate = CurrentDate, QuartersBack = 3)
CodePudding user response:
Could you use this to get the prior quarters?
library(tidyverse)
library(clock)
library(lubridate)
#>
#> Attaching package: 'lubridate'
#> The following object is masked from 'package:clock':
#>
#> as_date
#> The following objects are masked from 'package:base':
#>
#> date, intersect, setdiff, union
today <- date_today(Sys.timezone())
# Last 3 quarters
ceiling_date(today - months(3), "quarter") - 1
#> [1] "2022-03-31"
ceiling_date(today - months(6), "quarter") - 1
#> [1] "2021-12-31"
ceiling_date(today - months(9), "quarter") - 1
#> [1] "2021-09-30"
Created on 2022-05-18 by the reprex package (v2.0.1)
CodePudding user response:
The yearqtr class represents years and quarters as years plus fractions 0, 1/4, 1/2 and 3/4 so given a date this gives the prior 3 quarters as yearqtr objects. Below we used May 18, 2022 as the input and that corresponds to 2022 Q2.
If you want to convert that to the first or last date of the quarter as a Date object use as.Date(...)
for the start or as.Date(..., frac = 1)
for the end. If you want to convert them to character use format(...)
. See ?format.yearqtr
.
library(zoo)
input <- as.Date("2022-05-18")
as.yearqtr(input) - 1:3/4
## [1] "2022 Q1" "2021 Q4" "2021 Q3"