7

I have a text file that includes dates, and want to convert it to a datatable.

Converting the dates such as 03-FEB-2011 can be done with

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 can I change the locale so the %b abbreviation works for Spanish? Is there any other way to achieve this?

SabDeM
  • 7,050
  • 2
  • 25
  • 38
Maria Velasco
  • 191
  • 2
  • 3
  • 8
  • I think you should do `Sys.setlocale(locale = "en_US.UTF-8")` of course instead of `en_US.UTF-8` you have to use the right locale which you can find with `system("locale -a", intern = TRUE)`. Let me know if it works well. – SabDeM Aug 17 '15 at 18:25
  • which is `"es_ES.UTF-8"` in your case. – SabDeM Aug 17 '15 at 18:38
  • If you get "OS reports request to set locale to "LOCALE" cannot be honored". See https://askubuntu.com/questions/76013/how-do-i-add-locale-to-ubuntu-server – Brandon Bertelsen Dec 13 '18 at 18:13

2 Answers2

9

As my previous comment, here is a complete and tested answer. As I've said you have to set your locale to the one right for your data (in this case spanish).

The code that allows you to do that is the following:

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

you can see the complete list of available locales 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"
SabDeM
  • 7,050
  • 2
  • 25
  • 38
6

If you can't 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 package readr() 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"
Juan Zuluaga
  • 61
  • 1
  • 1