0

I transformed a date in the yyyy-mm-dd format to the yyyy-mm format using the following command:

format(as.Date(DATE, "%Y-%m-%d"), "%Y-%m")

This works but it returns a character. I want to further use this as a date, so I want to transform this character back to a date class. Using the as.Date() function gives me the error:

Error in charToDate(x) : character string is not in a standard unambiguous format

Does anyone know how to solve this problem?

Update: In the end I want to determine the number of months between two dates in the format yyyy-mm. Does anyone knows how to do this without transforming the characters back to date class?

Activation
  • 93
  • 6

1 Answers1

-1

I had once the same problem and unfortunately the only solution I found was to keep format YYYY-mm-dd with dd==01...

Here is part of the code if you want:

DATE <- str_c(DATE,"-01") df$date <- as.Date(DATE,format="%Y-%m-%d")
zgue
  • 3,793
  • 9
  • 34
  • 39
T. Ciffréo
  • 126
  • 10