5

I'm getting a result I don't understand in R.

If I use strptime with a year and day formatted %Y-%m (like "2009-12"), I get an NA result. But if I add a day, like "2009-12-01", and change the format string accordingly, I do get a result. Example:

> strptime("2009-12",format="%Y-%m")
[1] NA
> strptime("2009-12-03",format="%Y-%m-%d")
[1] "2009-12-03"

Why is that?

Update: The thing I'm curious about is why strptime doesn't parse a year and a month, and the reason it seems weird that it wouldn't do so is because it does parse a year only, or a year-and-a-day:

> strptime("2009",format="%Y") # year only. Works. Uses current month and day as defaults.
[1] "2009-12-02"
> strptime("2009-03",format="%Y-%d") # year and day. Works. Uses current month as default.
[1] "2009-12-03"
> strptime("2009-03",format="%Y-%m") # year and month. Doesn't work. ?
[1] NA

Update to explain why this is not a duplicate The possible duplicate was asked a few years after this question and it is concerned with a separate API in R: the asDate function. This question is about a quirk of the strptime function that as of R 3.1.3 still applies.

bantic
  • 4,886
  • 4
  • 29
  • 34

2 Answers2

1

That seems like sensible behavior to me. As I see it, a better question would be "why does it allow you to do this: strptime("2009-03",format="%Y-%d")?"

Here's a workaround to get what I think you're trying to achieve (i.e. a POSIXlt object with a specified month and year, but today's day):

as.POSIXlt(paste("2009-12", days(Sys.Date()), sep="-"))
Shane
  • 98,550
  • 35
  • 224
  • 217
  • So it's just a weird idiosyncrasy of the language? – bantic Dec 01 '09 at 22:37
  • 2
    No, its a requirement as a 'date' designates a day in a month in a year, and just a giving a month and a year is not equivalent. – Dirk Eddelbuettel Dec 01 '09 at 23:30
  • 1
    But then why does it work if you give it only a year, or only a year and a day? – bantic Dec 02 '09 at 15:53
  • My guess is that "that's just how it works" (in other words, there is no good reason). As everyone is saying, it doesn't make much sense for why you would actually want to do this at all. If you want today's date, use Sys.Date(). Otherwise just do what Shane suggested above... – griffin Dec 02 '09 at 16:20
  • 5
    I know this question is really old but I found it when I had the same question myself. According to the [documentation](http://stat.ethz.ch/R-manual/R-patched/library/base/html/strptime.html) on `strptime`: "For strptime the input string need not specify the date completely: it is assumed that unspecified seconds, minutes or hours are zero, and an unspecified year, month or day is the current one." So it should "seems like your workaround shouldn't be needed. – DQdlM Jun 05 '11 at 22:01
1

I'm just guessing here. But if it takes a year and a day, it's probably taking a year and a day in the range of 1-365 (or 366 for leap years). What you could do is use paste() and add -01 at the end to get the standard YYYY-MM-DD format.

Here's the test I ran.

strptime("2009-123",format="%Y-%d")

returns "2009-05-12"

Marek
  • 49,472
  • 15
  • 99
  • 121
duphenix
  • 11
  • 1