I have a character vector containing dates in the format "YYYY-MM", such as:
date_vec <- c("2020-01", "2020-02", "2020-03", "2020-04")
I want to convert this vector to a Date format in R, but I want to keep only the year and month information without adding any information about the day of the month. Specifically, I want the resulting Date objects to have the format "%Y-%m"
and not "%Y-%m-%d"
.
I have tried using the as.Date()
function in R with the format argument set to "%Y-%m"
, but this seems returns NA
.
as.Date(date_vec, format = "%Y-%m")
#return NA NA NA NA
Any thoughts, please?