[R] basic help with as.Date()

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu Mar 26 22:36:52 CET 2015

On 26/03/2015 21:11, Simon Kiss wrote:
> Hi there: normally I’m quite comfortable with as.Date(). But this data set is causing problems.
> The core of the data frame looks like the sample data frame below, but my attempt to convert df$mydate to a date object returns only NA. Can anyone provide a suggestion?
> Thank you, Simon Kiss
> #sample data frame
> df<-data.frame(mydate=factor(c('Jan-15', 'Feb-13', 'Mar-11', 'Jul-12')), other=rnorm(4, 3))
> #Attempt to convert
> as.Date(as.character(df$mydate), format='%b-%y')

You would be on surer ground with something like

as.Date(paste0('01-',as.character(df$mydate)), format='%d-%b-%y')

since it is unclear what dates you expected to get.

Brian D. Ripley,                  ripley at stats.ox.ac.uk
Emeritus Professor of Applied Statistics, University of Oxford
1 South Parks Road, Oxford OX1 3TG, UK

More information about the R-help mailing list