My data has a StartDate column, which includes the date the observation began a certain part of the study, ie patient A started treatment 1 on January 1st 2014 and began treatment 2 beginning June 1st 2014.
The rows leading (lagging) to the new treatment Date are blank. I would like to use an if_else statement to fill in the blank cells with the lagged Date. In the example below the 'StartDate' column is what I currently have and the 'NewDate' column is what I would like to end with. The cells "" are the blank, or NULL, cells that I would like filled with the lagged Date.
I chose to use the lag function because I was thinking that cell would chain off of each other ie cell three would copy cell 2, which would copy cell 1.
Any suggestions? Thanks
df<-data.frame(StartDate=c(as.Date('2014-01-01'),"","","","",as.Date('2014-06-01'),"","","","",""),
NewDate=c(as.Date('2014-01-01'),as.Date('2014-01-01'),as.Date('2014-01-01'),as.Date('2014-01- 01'),as.Date('2014-01-01'),as.Date('2014-06-01'),as.Date('2014-06-01'),as.Date('2014-06- 01'),as.Date('2014-06-01'),as.Date('2014-06-01'),as.Date('2014-06-01')))
df$testDate<-if_else(is.na(df$Date),lag(df$Date),df$Date)
Aucun commentaire:
Enregistrer un commentaire