R Read the abbreviated month with a date that is not in English.

I have a text file containing dates and you want to convert it to datatable.

Converting dates such as 03-FEB-2011 can be done using

data$fecha <- as.Date(data$textDate , "%d-%b-%Y") 

The problem is that the column is in Spanish, so I don't get Jan, but Ene, or Aug, but Ago. How do I change the language so that the abbreviation% b works in Spanish? Is there any other way to achieve this?

+5
source share
2 answers

Like my previous comment, here is a complete and verified answer. As I said, you need to set locale to the right for your data (in this case, Spanish).

The code that allows this is as follows:

 Sys.setlocale(locale="es_ES.UTF-8") 

you can see the full list of available locale with system("locale -a", intern = TRUE) (not sure if it works well on Windows systems).

Here is an example:

 x <- c("03-Ago-2011", "21-Ene-2012") as.Date(x, format = "%d-%b-%Y") [1] "2011-08-03" "2012-01-21" 
+4
source

If you cannot add locales to your OS,

 > Sys.setlocale(locale = "es") [1] "" Warning message: In Sys.setlocale(locale = "es") : OS reports request to set locale to "es" cannot be honored 

The readr () package has ways to specify and even create locales:

 > library(readr) > parse_date("31 DICIEMBRE 2011","%d %B %Y",locale=locale("es")) [1] "2011-12-31" 
+3
source

All Articles