I want to find a first date greater than the given date in a column. eg:
Pnp, Date1 Date2
A100,1/1/2020,1/1/2020
A100,1/1/2020,1/7/2020
A100,1/1/2020,1/1/2021
A100,1/1/2020,1/7/2021
Sample output:
Pnp,Date1,Date2,Date3,New Column
A100,1/1/2020,1/1/2020, 1/7/2020
A100,1/1/2020,1/7/2020,1/7/2020
A100,1/1/2020,1/1/2021,1/72020
A100,1/1/2020,1/7/2021,1/7/2020
I mean Based on date in date1 which date is greater than Date1
in Date2
(First greater value in Date2
) to be put in New column
.
sample code is :
library(dplyr)
library(sqldf)
monthly_sequence_03<- data.frame('Pnp' = 'A100','Frequency' = 3,'Duration' = c('Month'),'Date1' =seq(as.Date('2020-01-01'), as.Date('2025-6-30'), by = '3 months'))
monthly_sequence_06<- data.frame('Pnp' = 'A100','Frequency' = 6,'Duration' = c('Month'),'Date2' =seq(as.Date('2020-01-01'), as.Date('2025-6-30'), by = '6 months'))
new_df <- sqldf("select a.*,b.Date2 from monthly_sequence_03 as a
left join monthly_sequence_06 as b
on a.pnp = b.pnp")
new_df <-new_df[
order( new_df[,3], new_df[,4] ),
]
Any help is highly appreciated.