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.
CodePudding user response:
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 lubridate
s 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"
CodePudding user response:
You could use a dict
ionary, 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")