我正在使用 gdata
导入 xls
文件。我正在使用 as.Date
转换日期列来转换日期
根据 as.Date
的手册,日期来源取决于平台,因此我正在确定相应地使用哪个来源
.origin <- ifelse(Sys.info()[['sysname']] == "Windows", "1899-12-30", "1904-01-01")
as.Date(myData$Date, origin=.origin)
但是,我想知道是否应该考虑读取文件的平台或写入文件的平台?
就其值(value)而言,我目前正在没有 Excel 的 Linux 机器上测试代码,并且使用 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 。它将 Excel 日期加载到 POSIXct 中,根据文件是由 Windows 还是 Mac Excel 编写来正确选择来源。
关于r - 从 Excel 到 R 的日期,平台依赖性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15686451/