I have trouble converting a character column with YYYY-MM format to date types in R. I have already tried different approaches to address this but none worked.
dateC = "2001-01"
# Method I)
dateF <- as.factor(dateC) # returns factor: "2001-1"
dateD <- as.Date(dateF,format="%Y-%m") # returns NA
# Method II)
dateI <- as.double(dateC) # returns NA
dateD <- as.Date(dateI,format="%Y-%m") # returns NA
The method I) works when the character column is in YYYY-MM-DD format, however not for year and month ones.
Method II) is not working for either of the formats. I somehow managed to change the characters to integer (with some random function that I do not remember now!) and then applied as.date() but that did not work either.