将字符串转换为数字日期格式时出现问题R

时间:2019-11-13 12:39:11

标签: r date lubridate date-conversion

我想将具有以下格式21 Septiembre 2019的日期列转换为21/09/2019,但我一直遇到错误。

我正在使用的代码

这是包含日期的对象 parse_date_time

STM_silla_2$date                                     ```

我收到以下错误。可能是因为包裹无法识别西班牙语月份吗?

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

以下是一些数据示例

All formats failed to parse. No formats found

dput(STM_silla_2$date[1:5])

我也尝试了c(" 26 Septiembre 2016 ", " 06 Septiembre 2012 ", " 25 Octubre 2013 ", " 07 Septiembre 2015"," 19 Noviembre 2014 ")中的dmy(),但没有结果。

2 个答案:

答案 0 :(得分:2)

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

添加语言代码locale = locale(“ es”)

答案 1 :(得分:0)

将您的语言环境设置为西班牙语,然后转换为Date

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

之后,lubridate::dmy也可以工作

lubridate::dmy("21 Septiembre 2019")
#[1] "2019-09-21"