Home > Back-end >  Spanish dates in R
Spanish dates in R

Time:11-08

I have a vector of dates in R I must modify. The dates, however, are in Spanish. How do I work the data if it's not in English?

As an example, "Jan 02 1987" is "ene 02 1987"

CodePudding user response:

You can (temporarily) change your language to "Spanish" with Sys.setlocale(). For example, on my Windows machine, the following works

# My default locale is (Australian) English
x <- c("Jan 02 1987", "ene 02 1987")
as.Date(x, "%b %d %Y")
#[1] "1987-01-02" NA 

Sys.setlocale("LC_TIME", "Spanish")
as.Date(x, "%b %d %Y")
#[1] NA           "1987-01-02"

Based on your OS, you might have to use a slightly different Sys.setlocale() call. See this post for details.

CodePudding user response:

If Sys.setlocale doesn't work, you can use parse_datefunction from readr package.

For example,

parse_date(c("ene. 30 2016", "feb. 1 2017"), "%b %d %Y", locale = locale("es"))

will return

[1] "2016-01-30" "2017-02-01"

(month is printed in number because of my Sys.locale. It will not matter.)

If your vector does not have . after month,

x <- c("ene 30 2016", "feb 1 2017")    
parse_date(paste(paste0(word(x,1), "."), word(x,2,3)), "%b %d %Y", locale = locale("es"))

will work.

  • Related