【问题标题】:Issues converting strings to numeric date formats R将字符串转换为数字日期格式的问题 R
【发布时间】:2019-11-13 12:39:11
【问题描述】:

我想将具有以下格式的日期列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 ")

我也从:lubridate 尝试了dmy(),但没有结果。

【问题讨论】:

    标签: r date lubridate date-conversion


    【解决方案1】:

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

    添加语言代码 locale=locale("es")

    【讨论】:

      【解决方案2】:

      将您的语言环境设置为西班牙语,然后转换为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"
      

      【讨论】:

      • 我收到以下错误In Sys.setlocale("LC_ALL", "es_ES") : OS reports request to set locale to "es_ES" cannot be honored
      • @JoseDavid 你的操作系统是什么?它适用于我在 Mac 上。
      • 我的操作系统是 Windows
      • 我没有windows机器来测试这个但是你可以试试Sys.setlocale("LC_TIME", "Spanish")
      猜你喜欢
      • 2023-03-26
      • 2013-08-21
      • 1970-01-01
      • 2019-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-12
      • 1970-01-01
      相关资源
      最近更新 更多