I'm trying to get a column of my dataframe (ValuationDate) to reflect "quarter end" dates (12/31, 3/31, 6/30, 9/30 of the given year), based on bounding dates already provided.
So, I have an "Index" column (IDNum), a ".id" column (which counts the IDNum) and two date fields (Reporting Date and Settlement Date).
Basically, given a Report Date and a Settlement Date, i want to generate record for each quarter-end date between those two, plus the Report and Settlement Date themselves.
For example:
Report Date: 2001-12-29; Settlement Date: 2002-05-31
This should generate 4 records:
- 2001-12-29
- 2001-12-31
- 2002-03-31
- 2002-05-31
I've managed to replicate the rows the correct number of times, and can get the two "easy" dates in there (the first and the last for each record), but am struggling with the interim dates (the "What goes here" part of the code).
library(zoo)
ClaimID_sam <- "1x1"
ReptDat_sam <- strptime("2001-12-29", format = "%Y-%m-%d")
SettDat_sam <- strptime("2002-05-31", format = "%Y-%m-%d")
RecordCount <- as.integer((4*(as.yearqtr(SettDat_sam) - as.yearqtr(ReptDat_sam))) + 2)
sam_DF <- data.frame(ClaimID_sam,ReptDat_sam,SettDat_sam,RecordCount)
sam_DF <- as.data.frame(lapply(sam_DF,rep,RecordCount))
sam_DF = getanID(sam_DF,"ClaimID_sam")
sam_DF$ValDate <- ifelse(sam_DF$.id == 1,
as.Date(sam_DF$ReptDat_sam),
"WHAT GOES HERE?????")
sam_DF$ValDate = ifelse(sam_DF$.id == sam_DF$RecordCount,
as.Date(sam_DF$SettDat_sam),
sam_DF$ValDate)
EDIT @g-grothendieck 's solution is nearly perfect, though seeing some oddities, not quite hitting quarter end points?
> do.call("rbind", by(ModData, ModData$ClaimID, add_dates))
ClaimID Loss_Reported_Date settlementDate ValuationDate
11X1.1 11X1 2001-12-29 2002-05-31 2001-12-29 00:00:00
11X1.2 11X1 2001-12-29 2002-05-31 2001-12-30 18:00:00
11X1.3 11X1 2001-12-29 2002-05-31 2002-03-30 18:00:00
11X1.4 11X1 2001-12-29 2002-05-31 2002-05-31 00:00:00
11X2.1 11X2 2002-04-06 2002-10-04 2002-04-06 00:00:00
11X2.2 11X2 2002-04-06 2002-10-04 2002-06-29 19:00:00
11X2.3 11X2 2002-04-06 2002-10-04 2002-09-29 19:00:00
11X2.4 11X2 2002-04-06 2002-10-04 2002-10-04 00:00:00
as.yearqtr()come from? Please, provide the package. - Francesco Grossetti