0

I am Peruvian and I'm learning to program in R

when I convert a character to date using the function as.Date I get NA. This is because the abbreviated month does not have a point (Jul. Sep.)

fecha<-c("06-jul-2015","06-sep-2012")

as.Date(fecha, format="%d-%b-%Y")

[1] NA NA

Please. How I can do to R consider the abbreviated month without the point?

Thank You So Much

Community
  • 1
  • 1
user3605895
  • 31
  • 2
  • 7
  • It works for me. Please restart your R and try again. – user227710 Jul 18 '15 at 03:47
  • 1
    Month abbreviations are locale-dependent. Please run `Sys.getlocale()` and show us the output. You can also run `strftime(sprintf('2015-%02d-01',1:12),format='%b')` to get your locale's abbreviations directly. – bgoldst Jul 18 '15 at 04:10
  • This is the output > Sys.getlocale() [1]"LC_COLLATE=Spanish_Spain.1252;LC_CTYPE=Spanish_Spain.1252;LC_MONETARY=Spanish_Spain.1252;LC_NUMERIC=C;LC_TIME=Spanish_Spain.1252" > strftime(sprintf('2015-%02d-01',1:12),format='%b') [1] "ene." "feb." "mar." "abr." "may." "jun." "jul." "ago." "sep." "oct." "nov." "dic." My database has fields with dates as 01-Jul- 2014 (example).For this reason I was looking for the function "as.Date" obtains NA. Please. How I can do to R consider abbreviated month without dot? months(http://as.Date ("2015-07-06"), T) [1] "jul." thanks – user3605895 Jul 18 '15 at 04:30
  • Possible duplicate of http://stackoverflow.com/questions/15566875/as-date-returning-na-in-r – Henrik Jul 18 '15 at 07:59

1 Answers1

0

I had exactly the same problem yesterday. I just added the following line

Sys.setlocale("LC_TIME", "C")
fecha<-c("06-jul-2015","06-sep-2012")
as.Date(fecha, format="%d-%b-%Y")

and it works:

"2015-07-06" "2012-09-06"

Edit: for the Spanish and the dot in your comment:

Sys.setlocale("LC_TIME","Spanish")
fecha<-c("06-ene.-2015","06-dic.-2012")
as.Date(fecha, format="%d-%b.-%Y")
HOSS_JFL
  • 765
  • 2
  • 9
  • 24
  • Thank you!! It works with abbreviated months in english. But How is for case in spanish. What do I have that to modify in the function Sys.setlocale(). Because for my software R abbreviated months are "ene." "feb." "mar." "abr." "may." "jun." "jul." "ago." "sep." "oct." "nov." "dic." With a dot in the end – user3605895 Jul 18 '15 at 17:48