as.Date(as.POSIXct())给出错误的日期? [英] as.Date(as.POSIXct()) gives the wrong date?
问题描述
我一直在尝试查看一个数据框,提取POSIXct列的日期组件与某个值匹配的所有行。我遇到了以下错误,令我很困惑: as.Date (as.POSIXct(...))
并不总是返回正确的日期。
>
>
[1]2012-08-06 09:35:23 EST as.Date(dt)
[1]2012-08-05
为什么是2012-08- 06 09:35:23的日期是否等于2012-08- 05 ?
$ b $我怀疑这与使用的不同时区有关,所以注意到
dt
的时区是EST,我把它赋给了 as.Date
:: > as.Date(as.POSIXct('2012-08-06 09:35:23'),tz ='EST')
[1]2012-08-05
但它仍然返回2012-08-05。
为什么会这样?如何查找2012-08-06的数据帧中的所有datetimes? (as subset(my.df,as.character(as.Date(datetime),tz ='EST')=='2012-08-06')
不返回行日期时间 dt
即使这确实发生在2012-08-06 ...)?
添加的详细信息:Linux 64位(虽然可以在32位上重现),可以在R 3.0.1和3.0.0,我目前是AEST(澳大利亚东部标准时间)
时区的文档也让我感到困惑。在某些情况下,EST可能不是明确的,可能实际上是指澳大利亚的一个tz。尝试EST5EDT或America / New_York。
在这种情况下,它也可能与您未声明的操作系统处理'tz'参数的差异有关,因为我得到2012-08-06。 (我现在在PDT US tz,虽然我不知道这应该是重要的。)更改哪个函数获取tz参数可以澄清(或不):$ / $ $ $ $ $ $ $ $ $ $ $ $ $ pre> > as.Date(as.POSIXct('2012-08-06 19:35:23',tz ='EST'))
[1]2012-08-07
> as.Date(as.POSIXct('2012-08-06 17:35:23',tz ='EST'))
[1]2012-08-06
> as.Date(as.POSIXct('2012-08-06 21:35:23'),tz ='EST')
[1]2012-08-06
> as.Date(as.POSIXct('2012-08-06 22:35:23'),tz ='EST')
[1]2012-08-07
如果您从 as.POSIXct
省略tz,则假定UTC。
这些是Ozzie TZ的明确名称(至少在我的Mac上):
tzfile< - /usr/share/zoneinfo/zone.tab
tzones< - read.delim(tzfile,row.names = NULL,header = FALSE,
col.names = c(country,coords,name,comments),
as.is = TRUE,fill = TRUE,comment.char =#)
grep (^ Aus,tzones $ name,value = TRUE)
[1]澳大利亚/ Lord_Howe澳大利亚/霍巴特
[3]澳大利亚/柯里澳大利亚/墨尔本
[5]澳大利亚/悉尼澳大利亚/布鲁肯希尔
[7]澳大利亚/布里斯班澳大利亚/林德曼
[9]澳大利亚/阿德莱德澳大利亚/达尔文
[11]澳大利亚/珀斯澳大利亚/欧洲
I'd been trying to look through a dataframe extracting all rows where the date component of a POSIXct column matched a certain value.I came across the following which is confusing me mightily:: as.Date(as.POSIXct(...))
doesn't always return the correct date.
> dt <- as.POSIXct('2012-08-06 09:35:23')
[1] "2012-08-06 09:35:23 EST"
> as.Date(dt)
[1] "2012-08-05"
Why is the date of '2012-08-06 09:35:23' equal to '2012-08-05?
I suspect it's something to do with different timezones being used, so noting that the timezone of dt
was 'EST' I gave this to as.Date
::
> as.Date(as.POSIXct('2012-08-06 09:35:23'), tz='EST')
[1] "2012-08-05"
But it still returns 2012-08-05.
Why is this? How can I find all datetimes in my dataframe that were on the date 2012-08-06? (as subset(my.df, as.character(as.Date(datetime), tz='EST') == '2012-08-06')
does not return the row with datetime dt
even though this did occur on the date 2012-08-06...)?
Added details: Linux 64bit (though can reproduce on 32bit), can get this on both R 3.0.1 & 3.0.0, and I am currently AEST (Australian Eastern Standard Time)
The documentation for timezones is confusing to me too. In some case EST may not be unambiguous and may actually refer to a tz in Australia. Try "EST5EDT" or "America/New_York".
In this case it could also relate to differences in how your unstated OS handles the 'tz' argument, since I get "2012-08-06". ( I'm in PDT US tz at the moment, although I'm not sure that should matter. )Changing which function gets the tz argument may clarify (or not):
> as.Date(as.POSIXct('2012-08-06 19:35:23', tz='EST'))
[1] "2012-08-07"
> as.Date(as.POSIXct('2012-08-06 17:35:23', tz='EST'))
[1] "2012-08-06"
> as.Date(as.POSIXct('2012-08-06 21:35:23'), tz='EST')
[1] "2012-08-06"
> as.Date(as.POSIXct('2012-08-06 22:35:23'), tz='EST')
[1] "2012-08-07"
If you omit the tz from as.POSIXct
then UTC is assumed.
These are the unambiguous names of the Ozzie TZ's (at least on my Mac):
tzfile <- "/usr/share/zoneinfo/zone.tab"
tzones <- read.delim(tzfile, row.names = NULL, header = FALSE,
col.names = c("country", "coords", "name", "comments"),
as.is = TRUE, fill = TRUE, comment.char = "#")
grep("^Aus", tzones$name, value=TRUE)
[1] "Australia/Lord_Howe" "Australia/Hobart"
[3] "Australia/Currie" "Australia/Melbourne"
[5] "Australia/Sydney" "Australia/Broken_Hill"
[7] "Australia/Brisbane" "Australia/Lindeman"
[9] "Australia/Adelaide" "Australia/Darwin"
[11] "Australia/Perth" "Australia/Eucla"
这篇关于as.Date(as.POSIXct())给出错误的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!