Every once in a while I find a real head scratcher… Any ideas what is going on here?
> a = strptime("2003-11-01", "%Y-%m-%d")
> b = strptime("2004-01-31", "%Y-%m-%d")
> unlist(a)
sec min hour mday mon year wday yday isdst
0 0 0 1 10 103 6 304 0
> unlist(b)
sec min hour mday mon year wday yday isdst
0 0 0 31 0 104 6 30 0
> a$mon = a$mon-1
> b$mon = b$mon-1
> a=as.POSIXlt(as.POSIXct(a))
> b=as.POSIXlt(as.POSIXct(b))
> a
[1] NA
> b
[1] "2003-12-31 PST"
> unlist(a)
sec min hour mday mon year wday yday isdst
NA NA NA NA NA NA NA NA -1
> unlist(b)
sec min hour mday mon year wday yday isdst
0 0 0 31 11 103 3 364 0
Why can I edit b but not a? I feel like I must be missing something.
This (
a) works for me with the R and session details as below:Without any further information as per your locale and time zone settings etc, I would have to guess that in your locale/time zone that the date/time indicated by
aafter you subtracted 1 from the$monelement didn’t exist. R is pretty clever about these things but time zones and locales often catch people out.The real question is why are you using a date/time object when you are handling just dates?
a <- as.Date("2003-11-01", "%Y-%m-%d")would be sufficient in this example.Details of my R session: