Reputation: 853
I have a file with dates in the format YYYY-mm
. I am trying to prepare my data for a time series analysis and therefore need to convert the formats from factor to a Date
format.
What I've tried:
x <- '2011-11'
as.Date(as.character(a), "%Y-%m")
the last line gives me an output NA
.
Thank you!
Upvotes: 1
Views: 902
Reputation: 176648
"2011-11" is not a date (where's the day)? Either add a day:
a <- '2011-11'
as.Date(paste0(a,'-01'))
or use zoo's yearmon
class:
library(zoo)
as.yearmon(a)
Upvotes: 5
Reputation: 44614
'2011-11' isn't a date. Use paste(your.year.month.strs, '01', sep='-')
to add the day component to your strings, then call as.Date
with "%Y-%m-%d"
.
Upvotes: 2