Issues converting strings to numeric date formats R

261 views Asked by At

I want to convert a date column that has the following format 21 Septiembre 2019 into 21/09/2019 I have used parse_date_time but I keep getting an error.

Code I a am using

This is the object that contains the dates STM_silla_2$date

STM_silla_2$date <- parse_date_time(x=STM_silla_2$date, orders = c("dBY", "dBy")) ```

I get the following error. Could it be because the package does not recognize the months in Spanish?

All formats failed to parse. No formats found

Here is some data sample

dput(STM_silla_2$date[1:5])

c(" 26 Septiembre 2016 ", " 06 Septiembre 2012 ", " 25 Octubre 2013 ", " 07 Septiembre 2015"," 19 Noviembre 2014 ")

I have also tried dmy() from :lubridate without results.

2

There are 2 answers

4
Ronak Shah On BEST ANSWER

Set your locale to Spanish and then convert to Date

Sys.setlocale("LC_ALL","es_ES")
as.Date("21 Septiembre 2019", "%d %B %Y")
#[1] "2019-09-21"

After that lubridate::dmy would work as well

lubridate::dmy("21 Septiembre 2019")
#[1] "2019-09-21"
0
Sebin Sunny On

library(readr) 
parse_date(" 26  Septiembre  2016 ","%d %B %Y",locale=locale("es"))
#[1] "2016-09-26"

add the Language code locale=locale("es")