0
votes

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

1
It works for me. Please restart your R and try again.user227710
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
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(as.Date ("2015-07-06"), T) [1] "jul." thanksuser3605895

1 Answers

0
votes

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")