1

This returns NA instead of a Date object 2012-01.

as.Date(
    x = "2012-01",
    format = "%Y-%m"
)

Why does this return NA and what must I change to make it return a Date object representing January 2012?

Username
  • 3,463
  • 11
  • 68
  • 111

1 Answers1

1

Dates need to have a year, month, and day component. Assuming you were OK with the first of the month representing a given month, you could use:

x <- "2012-01"
d <- paste0(x, "-01")
as.Date(d, format="%Y-%m-%d")
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360