Sebastian Zeki Sebastian Zeki - 5 days ago 7
R Question

as.Date keeps on returning NAs

So I've been through the stack exchange answers and I can't resolve this

I have a column in a dataframe that has dates as characters as follows

2011-12
2012-04
2011-10

etc


I would like to convert these to date formats which I have tried to do as follows:

Tots$DatesMerge<-as.Date(Tots$DatesMerge,"%Y-%m"
)

but I get NA's back all the time

I tried to do as here but no joy. I'm really not sure what Im doing wrong.

Answer

I'd say as.Date won't be able to work on values where there's no day of the month. You could try with zoo, as long as you don't mind it coming out as a yearmon class:

library( zoo )
as.yearmon( Tots$DatesMerge )

Alternatively, you can specify a day of the month to use as a dummy:

as.Date( paste0( Tots$DatesMerge, "-15" ) )