从 excel 文件 R 导入数据框时如何处理复杂的日期格式
How to deal with complex date format when importing dataframe from an excel file R
我从 excel 文件中导入以下列。
dates <- structure(list(ServiceDate = c("44433", "44497", "44400", "44513",
"44501", "44398", "44501", "44496", "44432", "44513", "44389",
"44513", "44515", "44513", "44515", "NULL", "44501", "44432",
"44512", "44470")), row.names = c(NA, -20L), class = c("tbl_df",
"tbl", "data.frame"))
但是当我尝试进行一些更传统的日期转换时,我无法获得正确的格式。
anydate(date$ServiceDate)
[1] "4443-01-01" "4449-01-01" "4440-01-01" "4451-01-01" "4450-01-01" "4439-01-01" "4450-01-01" "4449-01-01" "4443-01-01" "4451-01-01" "4438-01-01"
[12] "4451-01-01" "4451-01-01" "4451-01-01" "4451-01-01" NA "4450-01-01" "4443-01-01" "4451-01-01" "4447-01-01"
我知道我可以更改实际文件中的格式,但有很多这样的文件,我需要将解决方案写在代码中。
这是我想要的输出
因为它是 excel 日期,转换为数字,并使用 as_date
和 origin
library(lubridate)
as_date(as.numeric(dates$ServiceDate), origin = '1899-12-30')
或者另一种方式是
openxlsx::convertToDate(dates$ServiceDate)
我从 excel 文件中导入以下列。
dates <- structure(list(ServiceDate = c("44433", "44497", "44400", "44513",
"44501", "44398", "44501", "44496", "44432", "44513", "44389",
"44513", "44515", "44513", "44515", "NULL", "44501", "44432",
"44512", "44470")), row.names = c(NA, -20L), class = c("tbl_df",
"tbl", "data.frame"))
但是当我尝试进行一些更传统的日期转换时,我无法获得正确的格式。
anydate(date$ServiceDate)
[1] "4443-01-01" "4449-01-01" "4440-01-01" "4451-01-01" "4450-01-01" "4439-01-01" "4450-01-01" "4449-01-01" "4443-01-01" "4451-01-01" "4438-01-01"
[12] "4451-01-01" "4451-01-01" "4451-01-01" "4451-01-01" NA "4450-01-01" "4443-01-01" "4451-01-01" "4447-01-01"
我知道我可以更改实际文件中的格式,但有很多这样的文件,我需要将解决方案写在代码中。
这是我想要的输出
因为它是 excel 日期,转换为数字,并使用 as_date
和 origin
library(lubridate)
as_date(as.numeric(dates$ServiceDate), origin = '1899-12-30')
或者另一种方式是
openxlsx::convertToDate(dates$ServiceDate)