Date formatted cell in xlsx files to R

你说的曾经没有我的故事 提交于 2019-12-13 03:12:55

问题


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

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))

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 ?


回答1:


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.

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: To convert an Excel datetime colum, see this (p.31)



来源:https://stackoverflow.com/questions/46469727/date-formatted-cell-in-xlsx-files-to-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!