首页 > 解决方案 > Convert dates from XLS into R

问题描述

I import dates from Excel into R.
Some of the values appear as real numbers:
e.g. 43313.458055555559
I would like to convert these numbers into a
timestamp representation like this - 9/4/2018 11:45:35AM

Thanks in advance.

标签: rexcel

解决方案


根据 G. Grothendieck 的评论,尝试:

as.POSIXct(as.Date(43313.458055555559, origin = "1899-12-30"))

只需替换43313.458055555559DataFrame.name$Column.name.

Windows PC 上的 Microsoft Excel 或 OpenOffice.org 等电子表格将日期时间表示为自 1899 年 12 月 30 日(通常)以来的天数和天数的分数。Ifx是此类数字的向量, as.Date("1899-12-30") + floor(x)则将给出相对于 Date 的起源的 Date 类日期的向量。同样chron("12/30/1899") + x 将给出相对于 chron 起源的 chron 日期。Mac 上的 Excel 通常将日期表示为自 1904 年 1 月 1 日以来的天数和小数天数as.Date("1904-01-01") + floor(x)并将chron("01/01/1904") + x表示此类日期的数字向量分别转换为 Date 和 chron。可以将 Excel 设置为使用任一来源,这就是上面通常使用该词的原因。

资料来源:https ://www.r-project.org/doc/Rnews/Rnews_2004-1.pdf


推荐阅读