0

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")
Jayloy Tsu
  • 31
  • 5

1 Answers1

1

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"
Duck
  • 39,058
  • 13
  • 42
  • 84