[R] basic help with as.Date()
Jeff Newmiller
jdnewmil at dcn.davis.CA.us
Thu Mar 26 22:42:08 CET 2015
That would be because they are not dates... they don't specify the day.
Either add a day of month to the character data before you convert it, or use the yearmon class from zoo.
Something like...
as.Date(paste("1",as.character(df$mydate),sep="-"), format='%d-%b-%y')
And why in the world are you converting to factor and back? Perhaps because you are not preventing conversion to factor when you read in your actual data? Do you know about the stringsAsFactors argument to read.table and friends?
---------------------------------------------------------------------------
Jeff Newmiller The ..... ..... Go Live...
DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go...
Live: OO#.. Dead: OO#.. Playing
Research Engineer (Solar/Batteries O.O#. #.O#. with
/Software/Embedded Controllers) .OO#. .OO#. rocks...1k
---------------------------------------------------------------------------
Sent from my phone. Please excuse my brevity.
On March 26, 2015 2:11:50 PM PDT, 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')
>
>______________________________________________
>R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list