2

I am working with a Feb 29 date in my dataset. I would like to change this date to Feb 28th when this occurs. I tried by using an ifelse statement to handle this situation, but the behavior is strange.

What I would like to accomplish is: if the month is 2, and the day of month is 29, then go back 1 day, else preserve the date.

When running:

  library(lubridate)
  dt<-ymd("2008-02-29")
  dt<-ifelse(month(dt)==2 & day(dt)==29,
                   dt-days(1),dt)
  dt

The above returns something very strange:

> dt
[1] 1204156800

However, when I run it without the ifelse statement the date math works just fine:

  dt<-ymd("2008-02-29")
  dt<-dt-days(1)
  dt
> dt
[1] "2008-02-28 UTC"

Does anyone have any thoughts on how to accomplish this behavior? Thank you!

Trexion Kameha
  • 3,362
  • 10
  • 34
  • 60
  • `if(month(dt)==2 & day(dt)==29) dt-days(1) else dt` works fine for me. – jbaums Jan 21 '15 at 15:51
  • I tend to use the `safe.ifelse` function when working with dates ; from the comments http://stackoverflow.com/questions/6668963/how-to-prevent-ifelse-from-turning-date-objects-into-numeric-objects#comment7892723_6669237 – user20650 Jan 21 '15 at 17:12

0 Answers0