【问题标题】:Converting dates from the format "01-Jan-2017" into something usable in R将日期从“01-Jan-2017”格式转换为 R 中可用的格式
【发布时间】:2017-07-13 00:22:36
【问题描述】:

我已将数据集导入 R,日期格式为 01-Jan-2017。我曾尝试使用诸如as.Date() 之类的函数,但由于它不是标准格式而无法识别。

我希望按日期对列进行排序,所以我希望有一种方法可以将当前格式转换为我可以在 R 中使用的格式。

提前致谢。

【问题讨论】:

  • lubridate::dmy("01-Jan-2017")

标签: r date format


【解决方案1】:

如果您无法阅读有关(在您的情况下为as.Date() 所需的)格式字符串%d-%b-%Y 的详细帮助页面,那么anytime 包的anydate() 函数是你的朋友:

R> library(anytime)
R> anydate("01-Jan-2017")
[1] "2017-01-01"
R> 

anydate() 和包中的其他函数(如 anytime()不需要格式,因为它们会为您尝试常用的。

【讨论】:

    【解决方案2】:

    as.Date() 方法应该适用于此,但您需要指定格式。

    date <- as.Date("01-Jan-2017", "%d-%b-%Y")
    

    【讨论】:

    • 我使用的是 R-3.3.3,Windows 7。
    猜你喜欢
    • 2023-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-12
    • 1970-01-01
    • 2020-08-15
    • 1970-01-01
    相关资源
    最近更新 更多