Month_Yr 2007-01 2008-03 2009-04
I want to convert these into a date data type. i used the as.Date function but it keeps printing out NA.
total_data$Month_Yr2 = as.Date(as.yearmon(total_data$Month_Yr), "%m-%Y")
Month_Yr 2007-01 2008-03 2009-04
I want to convert these into a date data type. i used the as.Date function but it keeps printing out NA.
total_data$Month_Yr2 = as.Date(as.yearmon(total_data$Month_Yr), "%m-%Y")
You would need a day to have the date, so you can add:
#Data
string <- '2007-01'
#Format
new <- as.Date(paste0(string,'-01'),'%Y-%m-%d')
And after that you can use format()
:
#Format 2
new2 <- format(new,"%m-%Y")
Ending up with:
[1] "01-2007"