【问题标题】:Unable to read dates in proper format using read.xlsx Package in R无法使用 R 中的 read.xlsx 包以正确格式读取日期
【发布时间】:2018-09-10 14:25:02
【问题描述】:

大家好!我一直在尝试使用 read.xlsx() 函数从 Excel 文件中将一些数据读入 R。我的日期范围从 1843 年开始。我尝试了在线提供的各种提示,但无法获得正确的日期格式。如果有人可以提供任何建议,那将是一个很大的帮助。非常感谢。

【问题讨论】:

  • 将日期导入为文本,然后使用as.POSIXct()转换为正确的格式。
  • 嘿!谢谢你,但日期列被作为一个因素读入。我发现很难找到如何进行的方法。知道我该怎么做吗?
  • 我对 readxl-package 有很好的体验。

标签: r excel readxl


【解决方案1】:
df <- tibble(
  DOB = c(19644, 29571, 19744, 22557, 28258, 24366)
)



df_2 <- df %>%
mutate(DOB = DOB %>% 
       as.character %>%     # First convert the factors into character strings
       as.numeric %>%       # ...and then to numeric days since origin
       as.Date(origin = "1899-12-30")  # ...and finally to a date
     )
# Note: for Excel on Mac, the origin is "1904-01-01" because Think Different.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-25
    • 2016-11-17
    • 2017-04-10
    • 1970-01-01
    • 1970-01-01
    • 2014-09-29
    • 1970-01-01
    • 2022-07-18
    相关资源
    最近更新 更多