Search code examples
ras.date

as.Date returns NA if applied to a list


I have want R to recognize this list:

 [1] "Ago12" "Ago13" "Ago14" "Ago15" "Ago16" "Ago17" "Ago18"
 [8] "Apr12" "Apr13" "Apr14" "Apr15" "Apr16" "Apr17" "Apr18"
[15] "Apr19" "Dic12" "Dic13" "Dic14" "Dic15" "Dic16" "Dic17"
[22] "Dic18" "Feb12" "Feb13" "Feb14" "Feb15" "Feb16" "Feb17"
[29] "Feb18" "Feb19" "Gen12" "Gen13" "Gen14" "Gen15" "Gen16"
[36] "Gen17" "Gen18" "Gen19" "Giu12" "Giu13" "Giu14" "Giu15"
[43] "Giu16" "Giu17" "Giu18" "Giu19" "Lug12" "Lug13" "Lug14"
[50] "Lug15" "Lug16" "Lug17" "Lug18" "Lug19" "Mag12" "Mag13"
[57] "Mag14" "Mag15" "Mag16" "Mag17" "Mag18" "Mag19" "Mar12"
[64] "Mar13" "Mar14" "Mar15" "Mar16" "Mar17" "Mar18" "Mar19"
[71] "Nov12" "Nov13" "Nov14" "Nov15" "Nov16" "Nov17" "Nov18"
[78] "Ott12" "Ott13" "Ott14" "Ott15" "Ott16" "Ott17" "Ott18"
[85] "Set12" "Set13" "Set14" "Set15" "Set16" "Set17" "Set18"

as a Date vector to use it to plot some series. When I run

time<-as.Date(datecorr,format="%b%y") I get NAs. I also tried to change the system language and time with Sys.setlocale("LC_TIME","it_IT.UTF-8") , but still it doesn't work.

I wonder what's wrong in what I'm doing

Thanks for help!

Edit: Thank you guys it works well


Solution

  • Assuming the input shown reproducibly in the Note at the end set the locale and then use as.yearmon and as.Date with the indicated format string. You could consider omitting the as.Date part since it is really a year/month you are trying to represent. scale_x_yearmon() can be used with ggplot2.

    library(zoo)
    Sys.setlocale(locale = "Italian")
    as.Date(as.yearmon(x, format = "%b%y"))
    

    giving:

     [1] "2012-08-01" "2013-08-01" "2014-08-01" "2015-08-01" "2016-08-01"
     [6] "2017-08-01" "2018-08-01" "2012-04-01" "2013-04-01" "2014-04-01"
    [11] "2015-04-01" "2016-04-01" "2017-04-01" "2018-04-01" "2019-04-01"
    [16] "2012-12-01" "2013-12-01" "2014-12-01" "2015-12-01" "2016-12-01"
    [21] "2017-12-01" "2018-12-01" "2012-02-01" "2013-02-01" "2014-02-01"
    [26] "2015-02-01" "2016-02-01" "2017-02-01" "2018-02-01" "2019-02-01"
    [31] "2012-01-01" "2013-01-01" "2014-01-01" "2015-01-01" "2016-01-01"
    [36] "2017-01-01" "2018-01-01" "2019-01-01" "2012-06-01" "2013-06-01"
    [41] "2014-06-01" "2015-06-01" "2016-06-01" "2017-06-01" "2018-06-01"
    [46] "2019-06-01" "2012-07-01" "2013-07-01" "2014-07-01" "2015-07-01"
    [51] "2016-07-01" "2017-07-01" "2018-07-01" "2019-07-01" "2012-05-01"
    [56] "2013-05-01" "2014-05-01" "2015-05-01" "2016-05-01" "2017-05-01"
    [61] "2018-05-01" "2019-05-01" "2012-03-01" "2013-03-01" "2014-03-01"
    [66] "2015-03-01" "2016-03-01" "2017-03-01" "2018-03-01" "2019-03-01"
    [71] "2012-11-01" "2013-11-01" "2014-11-01" "2015-11-01" "2016-11-01"
    [76] "2017-11-01" "2018-11-01" "2012-10-01" "2013-10-01" "2014-10-01"
    [81] "2015-10-01" "2016-10-01" "2017-10-01" "2018-10-01" "2012-09-01"
    [86] "2013-09-01" "2014-09-01" "2015-09-01" "2016-09-01" "2017-09-01"
    [91] "2018-09-01"
    

    Note

     Lines <- '"Ago12" "Ago13" "Ago14" "Ago15" "Ago16" "Ago17" "Ago18"
     "Apr12" "Apr13" "Apr14" "Apr15" "Apr16" "Apr17" "Apr18"
     "Apr19" "Dic12" "Dic13" "Dic14" "Dic15" "Dic16" "Dic17"
     "Dic18" "Feb12" "Feb13" "Feb14" "Feb15" "Feb16" "Feb17"
     "Feb18" "Feb19" "Gen12" "Gen13" "Gen14" "Gen15" "Gen16"
     "Gen17" "Gen18" "Gen19" "Giu12" "Giu13" "Giu14" "Giu15"
     "Giu16" "Giu17" "Giu18" "Giu19" "Lug12" "Lug13" "Lug14"
     "Lug15" "Lug16" "Lug17" "Lug18" "Lug19" "Mag12" "Mag13"
     "Mag14" "Mag15" "Mag16" "Mag17" "Mag18" "Mag19" "Mar12"
     "Mar13" "Mar14" "Mar15" "Mar16" "Mar17" "Mar18" "Mar19"
     "Nov12" "Nov13" "Nov14" "Nov15" "Nov16" "Nov17" "Nov18"
     "Ott12" "Ott13" "Ott14" "Ott15" "Ott16" "Ott17" "Ott18"
     "Set12" "Set13" "Set14" "Set15" "Set16" "Set17" "Set18"'
    x <- scan(text = Lines, what = "", quiet = TRUE)