[R] as.Date question

0 views
Skip to first unread message

MAL

unread,
Dec 20, 2009, 12:06:17 PM12/20/09
to r-h...@r-project.org
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!
[[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.

Jason Morgan

unread,
Dec 20, 2009, 12:14:07 PM12/20/09
to MAL, r-h...@r-project.org
Hello,

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

MAL

unread,
Dec 20, 2009, 12:18:21 PM12/20/09
to r-h...@r-project.org
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!

______________________________________________

Felix Andrews

unread,
Dec 21, 2009, 7:49:19 AM12/21/09
to Jason Morgan, r-h...@r-project.org
I don't know the details of why this is happening, but in any case it
is a good idea to round() your times if you want to convert them to
dates. Just as you would round() a numeric value to convert it to an
integer.

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/

Marek Janad

unread,
Dec 23, 2009, 6:08:33 PM12/23/09
to r-h...@r-project.org
Look at documentation

?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

MAL

unread,
Dec 24, 2009, 10:02:20 AM12/24/09
to r-h...@r-project.org
Mark, not sure that's the answer.

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?

Linlin Yan

unread,
Dec 24, 2009, 12:00:40 PM12/24/09
to MAL, r-h...@r-project.org
I am afraid that although in same literally, they are indeed different
functions: as.Date.POSIXct and as.Date.POSIXlt. But I am not sure why
they are designed like this, which causes the confusion as you
mentioned.

Gabor Grothendieck

unread,
Dec 24, 2009, 12:58:55 PM12/24/09
to MAL, r-h...@r-project.org
zzz1 is POSIXct so looking at:

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

Reply all
Reply to author
Forward
0 new messages