This piece of code:
zzz1 <- as.POSIXct("1999-03-18", tz="CET")
zzz2 <- as.POSIXlt("1999-03-18", tz="CET")
zzz1 == zzz2
as.Date(zzz1)
as.Date(zzz2)
yields TRUE for "zzz1==zzz2", but the two dates returned by as.Date are different:
> as.Date(zzz1)
[1] "1999-03-17"
> as.Date(zzz2)
[1] "1999-03-18"
I'm using R 2.10.0.
Would be glad for any clarifications. Thanks!
[[alternative HTML version deleted]]
______________________________________________
R-h...@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
On 2009.12.20 18:06:17, MAL wrote:
> All!
>
> This piece of code:
>
> zzz1 <- as.POSIXct("1999-03-18", tz="CET")
> zzz2 <- as.POSIXlt("1999-03-18", tz="CET")
> zzz1 == zzz2
> as.Date(zzz1)
> as.Date(zzz2)
>
> yields TRUE for "zzz1==zzz2", but the two dates returned by as.Date are different:
>
> > as.Date(zzz1)
> [1] "1999-03-17"
> > as.Date(zzz2)
> [1] "1999-03-18"
>
> I'm using R 2.10.0.
>
> Would be glad for any clarifications. Thanks!
I don't know why as.Date() is giving different results, but if look at
the value of the variables, they are equal:
> zzz1 <- as.POSIXct("1999-03-18", tz="CET")
> zzz2 <- as.POSIXlt("1999-03-18", tz="CET")
> zzz1 == zzz2
[1] TRUE
> as.Date(zzz1)
[1] "1999-03-17"
> as.Date(zzz2)
[1] "1999-03-18"
> zzz1
[1] "1999-03-18 CET"
> zzz2
[1] "1999-03-18 CET"
Maybe someone here can explain the behavior of as.Date().
Cheers,
~Jason
--
Jason W. Morgan
Graduate Student
Department of Political Science
*The Ohio State University*
154 North Oval Mall
Columbus, Ohio 43210
This piece of code:
zzz1 <- as.POSIXct("1999-03-18", tz="CET")
zzz2 <- as.POSIXlt("1999-03-18", tz="CET")
zzz1 == zzz2
as.Date(zzz1)
as.Date(zzz2)
yields TRUE for "zzz1==zzz2", but the two dates returned by as.Date are
different:
> as.Date(zzz1)
[1] "1999-03-17"
> as.Date(zzz2)
[1] "1999-03-18"
I'm using R 2.10.0.
Would be glad for any clarifications. Thanks!
______________________________________________
as.Date(round(zzz1, "days"))
2009/12/21 Jason Morgan <jwm-r...@skepsi.net>:
--
Felix Andrews / 安福立
Postdoctoral Fellow
Integrated Catchment Assessment and Management (iCAM) Centre
Fenner School of Environment and Society [Bldg 48a]
The Australian National University
Canberra ACT 0200 Australia
M: +61 410 400 963
T: + 61 2 6125 4670
E: felix....@anu.edu.au
CRICOS Provider No. 00120C
--
http://www.neurofractal.org/felix/
?as.Date
as.Date first represents time in UTC, what gives:
as.POSIXlt(zzz1, tz="UTC")
HTH
2009/12/20 MAL <dive...@univecom.ch>:
--
Marek
Usually one has x=y --> f(x)=f(y)
which doesn't seem to hold here (put x=zzz1, y=zzz2, f=as.Date()).
Or do I overlook something?
> as.Date.POSIXct
function (x, ...)
{
z <- floor(unclass(x)/86400)
attr(z, "tzone") <- NULL
structure(z, class = "Date")
}
<environment: namespace:base>
we see as.Date.POSIXct takes the POSIXct object, zzz1, and converts it
to Date relative to GMT. There is no time zone argument on
as.Date.POSIXct and the time zone specification given to it is
ignored.
On the other hand as.Date.POSIXlt takes the POSIXlt object, zzz2, and
presumably just uses the components in it:
> str(unclass(zzz2))
List of 9
$ sec : num 0
$ min : int 0
$ hour : int 0
$ mday : int 18
$ mon : int 2
$ year : int 99
$ wday : int 4
$ yday : int 76
$ isdst: int 0
- attr(*, "tzone")= chr "CET"
Note that as.Date.POSIXlt also has not time zone argument so any time
zone argument given to it is also ignored:
> as.Date.POSIXlt
function (x, ...)
.Internal(POSIXlt2Date(x))
<environment: namespace:base>
Perhaps the unexpected part is that as.Date.POSIXct always converts
relative to GMT so if you want to convert relative to anything else
its best to convert to character representation in the desired time
zone and then convert that to Date.
Also if you are dealing with dates that do not have times its best not
to use POSIXt in the first place. Date class is a better fit.
See relevant article in R News 4/1.