将xlsx文件中的日期格式的单元格日期转换为R [英] Date formatted cell in xlsx files to R

查看:105
本文介绍了将xlsx文件中的日期格式的单元格日期转换为R的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Excel文件,其中某些单元格中包含日期信息。像:

I have an excel file which has date information in some cells. like :

我通过以下命令将该文件读入R:

I read this file into R by the following command :

library(xlsx)

data.files = list.files(pattern = "*.xlsx")
data <- lapply(data.files, function(x) read.xlsx(x, sheetIndex = 9,header = T))

除带日期的单元格外,其他所有内容均正确!而不是将xlsx信息放入这些单元格中,我始终以 42948 作为日期:

Everything is correct except the cells with date! instead of having the xlsx information into those cell, I always have 42948 as a date :

有人知道我该怎么解决吗?

Does anybody know how could I fix this ?

推荐答案

如您所见,导入文件后,日期以数字值表示(此处为 42948 )。它们实际上是Excel中日期信息的内部表示。这些值是R代表的值,而不是实际日期。

As you can see, after importing your files, dates are represented as numeric values (here 42948). They are actually the internal representation of the date information in Excel. Those values are the ones that R presents instead of the "real" dates.

您可以使用 as.Date(42948-25569,origin = 1970-01-01)获得R中的那些日期。 c $ c>

You can get those dates in R with as.Date(42948 - 25569, origin = "1970-01-01")

请注意,您也可以使用包含日期内部表示形式的向量,因此这也应该有效

Notice that you can also use a vector containing the internal representation of the dates, so this should also work

vect <- c(42948, 42949, 42950)
as.Date(vect - 25569, origin = "1970-01-01")

PS:要转换Excel日期时间列,请参见(第31页)

PS: To convert an Excel datetime colum, see this (p.31)

这篇关于将xlsx文件中的日期格式的单元格日期转换为R的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆