title: Dynamic If_else statement w/ Dates and Lags: If_else Not Working in R
body: <p>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. </p>
  
  <p>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. </p>
  
  <p>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.</p>
  
  <p>Any suggestions? Thanks</p>
  
  <pre><code>df&lt;-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&lt;-if_else(is.na(df$Date),lag(df$Date),df$Date)
  </code></pre>