I am currently working with dates in R and need to calculate the time difference between two quarters. I have used the zoo
library to transform my dates into quarterly format, but I am struggling to calculate the difference between my dates.
Here is a sample code for reproducability:
sample_dataframe <- data.frame(First_Purchase_date = c(as.Date("2020-01-15"), as.Date("2019-02-10"),as.Date("2018-12-24")),Recent_Purchase_date = c(as.Date("2020-06-20"), as.Date("2020-10-10"), as.Date("2019-05-26")))
library(zoo)
#using zoo library to transform my dates into quarters
sample_dataframe$First_purchase_quarter <- as.yearqtr((sample_dataframe$First_Purchase_date), "%Y-%m-%d")
sample_dataframe$Recent_Purchase_quarter <- as.yearqtr((sample_dataframe$Recent_Purchase_date), "%Y-%m-%d")
What I want to achieve is to subtract Recent_Purchase_quarter from First_purchase_quarter to get a time difference in quarters.
So if Recent_Purchase_quarter is 2019 Q2 and First_Purchase_quarter is 2018 Q4 the result should be 2.
What would be the easiest way to get the time difference in quarters as described above?