How to convert Excel date format to proper date in R

I'm working with a csv which unfortunately has logged datetimes using the number format of 42705 although it should be 01/12/2016.

I'd like to convert it to the right format in R using lubridate or some other package. Is there a function that will handle it?

122811 次浏览

您不需要使用 lubridate,基函数 as.Date可以很好地处理这种类型的转换。诀窍是您必须提供原点,在 Excel 中是1899年12月30日。

as.Date(42705, origin = "1899-12-30")
# [1] "2016-12-01"

If you want to preserve your column types, you can try using the read_excel function from the readxl package. That lets you load an XLS or XLSX file with the number formatting preserved.

编辑: 相关 XKCD

XKCD comic strip

下面是另一种使用 janitor 和 tibble 包实现的方法:

install.packages("janitor")
install.packages("tibble")


library(tibble)
library(janitor)


excel_numeric_to_date(as.numeric(as.character(YourDate)), date_system = "modern")

openxlsx包还允许 xls date转换:

openxlsx::convertToDate(42705)
[1] "2016-12-01"

正如@Suren 所建议的,convertToDateTime允许 datetime转换:

openxlsx::convertToDateTime(42705.5)
[1] "2016-12-01 12:00:00"

正如人们所说,非常好的选择:

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


openxlsx::convertToDate(42705)

另一种方式也可以是:

format(as.Date(as.Date("1899-12-30") + 42705, "%d-%m-%Y"), "%d-%m-%Y")

注意,您可以更改写入 %d-%m-%Y的输出格式

(首先,转换 as.numeric,如果它被导入为字符! ,或转换在公式:

format(as.Date(as.Date("1899-12-30") + as.numeric( number formatted as character), "%d-%m-%Y"), "%d-%m-%Y")

如果使用 data.table 包,可以使用 as.IDate():

require(data.table)


as.IDate(42705, origin = "1899-12-30")
# [1] "2016-12-01"

就像这里的 base::as.Date()