[R] basic help with as.Date()

Marc Schwartz marc_schwartz at me.com
Thu Mar 26 22:38:11 CET 2015


> On Mar 26, 2015, at 4:11 PM, Simon Kiss <sjkiss at gmail.com> 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')


Hi,

R's default date class object requires a full date, with a month, day and year. 

You might look at the 'zoo' package on CRAN, which has a yearmon() function.

Regards,

Marc Schwartz



More information about the R-help mailing list