1

I am trying to convert the following to dates - where I have the day in numeric, the month in text and year in numeric.

dates = c("26 december, 2014", "23 november, 2018", "1 October, 2007", "31 august, 2013")

EDIT:

dates2 = c("13 marzo, 2017", "22 junio, 2018", "24 octubre, 2017", "19 julio, 2018", "14 septiembre, 2017", "3 diciembre, 2015", "23 enero, 2018", "26 septiembre, 2016", "2 agosto, 2017", "3 enero, 2018")
parse_date_time(dates, orders= "dmy")

Gives:

 [1] NA NA NA NA NA NA NA NA NA NA
Warning message:
All formats failed to parse. No formats found.
jay.sf
  • 60,139
  • 8
  • 53
  • 110
user113156
  • 6,761
  • 5
  • 35
  • 81
  • 1
    Use `lubridate::dmy(dates)` – akrun Apr 21 '22 at 16:56
  • I get a warning for other language dates – user113156 Apr 21 '22 at 17:03
  • that could be related to locale settings – akrun Apr 21 '22 at 17:09
  • `Sys.getlocale() [1] "LC_CTYPE=es_ES.UTF-8;LC_NUMERIC=C;LC_TIME=ca_ES.UTF-8;LC_COLLATE=ca_ES.UTF-8;LC_MONETARY=ca_ES.UTF-8;LC_MESSAGES=es_ES.UTF-8;LC_PAPER=es_ES.UTF-8;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=ca_ES.UTF-8;LC_IDENTIFICATION=C"` – user113156 Apr 21 '22 at 17:10
  • 1
    If you have different languages, may be the set the locale to that language. i.e. you can use the https://stackoverflow.com/questions/39340185/how-to-set-the-default-language-of-date-in-r for changing the locale – akrun Apr 21 '22 at 17:14
  • @akrun your `parsedate` answer works with spanish `dates2` on my machine, allthough my language is not spanish. So I think you should undelete! – TarJae Apr 21 '22 at 17:53
  • 1
    @TarJae it is actually not working with some other languages – akrun Apr 21 '22 at 18:00

3 Answers3

2

Update: for spanish dates:

Sys.setlocale("LC_TIME", "Spanish_Spain.1252")
format <- "%a@%A@%b@%B@%p@"
enc2utf8(unique(format(lubridate:::.date_template, format = format)))
str(lubridate:::.get_locale_regs("Spanish_Spain.1252"))

library(lubridate)

dates2 = c("13 marzo, 2017", "22 junio, 2018", "24 octubre, 2017", "19 julio, 2018", "14 septiembre, 2017", "3 diciembre, 2015", "23 enero, 2018", "26 septiembre, 2016", "2 agosto, 2017", "3 enero, 2018")

parse_date_time(dates2, orders= "dmy")

output: spanish dates:

 [1] "2017-03-13 UTC" "2018-06-22 UTC" "2017-10-24 UTC"
 [4] "2018-07-19 UTC" "2017-09-14 UTC" "2015-12-03 UTC"
 [7] "2018-01-23 UTC" "2016-09-26 UTC" "2017-08-02 UTC"
[10] "2018-01-03 UTC"

First answer:

In addition to akrun's answer in comments lubridate::dmy(dates) We could use lubridates parse_date_time function. Essential is to apply the orders, here day month year:

library(lubridate)

parse_date_time(dates, orders= "dmy")
[1] "2014-12-26 UTC" "2018-11-23 UTC" "2007-10-01 UTC"
[4] "2013-08-31 UTC"
TarJae
  • 72,363
  • 6
  • 19
  • 66
2

Rather than fiddling with the system locale, you could just use the clock package with a Spanish locale object. It has built in support for quite a few of the most common locales, and you can always create your own custom locale object with clock_locale() if the built in support isn't good enough.

This also has the benefit of being cross-platform. You don't have to worry about different locale strings for macOS vs Windows.

library(clock)

dates <- c(
  "13 marzo, 2017", "22 junio, 2018", "24 octubre, 2017", 
  "19 julio, 2018", "14 septiembre, 2017", "3 diciembre, 2015", 
  "23 enero, 2018", "26 septiembre, 2016", "2 agosto, 2017", "3 enero, 2018"
)

# Spanish weekday/month labels
clock_labels_lookup("es")
#> <clock_labels>
#> Weekdays: domingo (dom.), lunes (lun.), martes (mar.), miércoles (mié.), jueves
#>           (jue.), viernes (vie.), sábado (sáb.)
#> Months:   enero (ene.), febrero (feb.), marzo (mar.), abril (abr.), mayo (may.),
#>           junio (jun.), julio (jul.), agosto (ago.), septiembre
#>           (sept.), octubre (oct.), noviembre (nov.), diciembre (dic.)
#> AM/PM:    a. m./p. m.

date_parse(
  x = dates,
  format = "%d %B, %Y",
  locale = clock_locale(labels = "es")
)
#>  [1] "2017-03-13" "2018-06-22" "2017-10-24" "2018-07-19" "2017-09-14"
#>  [6] "2015-12-03" "2018-01-23" "2016-09-26" "2017-08-02" "2018-01-03"

Created on 2022-04-21 by the reprex package (v2.0.1)

Davis Vaughan
  • 2,780
  • 9
  • 19
1

You could use a dictionary, gsub in this case Spanish to English in a for loop, and convert as.Date.

dict <- structure(list(Spanish = c("enero", "febrero", "marzo", "abril",
"mayo", "junio", "julio", "agosto", "septiembre", "octubre",
"noviembre", "diciembre"), English = c("January", "February",
"March", "April", "May", "June", "July", "August", "September",
"October", "November", "December")), class = "data.frame", row.names = c(NA,
-12L))

for (i in seq_len(nrow(dict))) {
  dates2 <- with(dict, gsub(Spanish[i], English[i], dates2))
}

as.Date(dates2, '%d %B, %Y')
# [1] "2017-03-13" "2018-06-22" "2017-10-24" "2018-07-19" "2017-09-14" "2015-12-03"
# [7] "2018-01-23" "2016-09-26" "2017-08-02" "2018-01-03"

Data:

dates2 <- c("13 marzo, 2017", "22 junio, 2018", "24 octubre, 2017", "19 julio, 2018", 
            "14 septiembre, 2017", "3 diciembre, 2015", "23 enero, 2018", 
            "26 septiembre, 2016", "2 agosto, 2017", "3 enero, 2018")
jay.sf
  • 60,139
  • 8
  • 53
  • 110