【问题标题】:How can I get R to preserve dates from Excel? [duplicate]如何让 R 保留 Excel 中的日期? [复制]
【发布时间】:2022-07-07 00:03:51
【问题描述】:

我有一个数据集,其中有一列研究 ID 和参与日期。在 Excel 中,它看起来像:

  Study_ID      Dates
1      100 2014-06-03
2      200 2013-07-11
3      300 2013-12-04
4      400 2012-12-14
5      500 2013-10-10

但是,当我使用 readxl 包中的 read_xlsx 函数将 excel 文件读入 R 时,它会将日期转换为数字:

  Study_ID  Dates
1      100  41654
2      200  41526
3      300  41138
4      400  41829
5      500  41698

我认为 read_xlsx 函数应该保留日期,所以我不明白发生了什么或如何解决这个问题。它确实说“日期”被视为一个字符。

我该如何解决这个问题?

【问题讨论】:

    标签: r excel date character


    【解决方案1】:

    我们可以使用openxlsx中的convertToDate

    df1$Dates <- openxlsx::convertToDate(df1$Dates)
    

    【讨论】:

      猜你喜欢
      • 2021-03-13
      • 2020-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-15
      • 2018-03-03
      • 1970-01-01
      • 2021-11-22
      相关资源
      最近更新 更多