从Excel到R的日期,平台依赖 [英] Dates from Excel to R, platform dependency
问题描述
我正在使用 gdata
导入 xls
文件。我使用 as.Date
转换日期列转换日期
I'm importing xls
files using gdata
. I am converting date columns using as.Date
to convert the date
根据<$ c $的手册c> as.Date ,日期来源依赖于平台,因此我决定使用哪个起始点。
As per the manual for as.Date
, the date origin is platform dependent, and so I am determining which origin to use accordingly
.origin <- ifelse(Sys.info()[['sysname']] == "Windows", "1899-12-30", "1904-01-01")
as.Date(myData$Date, origin=.origin)
但是,我想知道如果我应该考虑文件是 的平台,或者写在 的平台?
However, I'm wondering if I should be considering the platform where the file is being read or the platform where it was written?
对于什么是值得的,我目前正在测试没有excel的linux框上的代码,正确的日期是通过使用 origin =1904-01-01生成的
For what it's worth, I am currently testing the code on a linux box with no excel, and the correct Dates are produced by using origin="1904-01-01"
引用`?as.Date'
## date given as number of days since 1900-01-01 (a date in 1989)
as.Date(32768, origin = "1900-01-01")
## Excel is said to use 1900-01-01 as day 1 (Windows default) or
## 1904-01-01 as day 0 (Mac default), but this is complicated by Excel
## treating 1900 as a leap year.
## So for dates (post-1901) from Windows Excel
as.Date(35981, origin = "1899-12-30") # 1998-07-05
## and Mac Excel
as.Date(34519, origin = "1904-01-01") # 1998-07-05
## (these values come from http://support.microsoft.com/kb/214330)
推荐答案
您可以尝试(非常)新的exell软件包: https://github.com/hadley/exell 。它将优先日期加载到POSIXct中,根据文件是由Windows或Mac Excel编写的,正确选择原点。
You could try out the (extremely) new exell package: https://github.com/hadley/exell. It loads excel dates into POSIXct, correctly choosing the origin based on whether the file was written by Windows or Mac Excel.
这篇关于从Excel到R的日期,平台依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!