Benjamin B.
2011-Jan-06 09:30 UTC
[R] Problem with timeSequence {timeDate} - wrong end date
Dear help-list, I have a problem with timeSequence {timeDate}. When I use it like> timeSequence(from = "2008-01-01", to = "2010-12-13", by = "1 month")GMT [1] [2008-01-01] [2008-02-01] [2008-03-01] [2008-04-01] [2008-05-01] [2008-06-01] [2008-07-01] [2008-08-01] [2008-09-01] [2008-10-01] [2008-11-01] [12] [2008-12-01] [2009-01-01] [2009-02-01] [2009-03-01] [2009-04-01] [2009-05-01] [2009-06-01] [2009-07-01] [2009-08-01] [2009-09-01] [2009-10-01] [23] [2009-11-01] [2009-12-01] [2010-01-01] [2010-02-01] [2010-03-01] [2010-04-01] [2010-05-01] [2010-06-01] [2010-07-01] [2010-08-01] [2010-09-01] [34] [2010-10-01] [2010-11-01] [2010-12-01] The result is as expected: a list of dates with all dates smaller then the "to" date. But somehow it behaves strange when I use it with a different starting date:> test <- timeSequence(from = "2008-01-15", to = "2010-12-13", by = "1month") GMT [1] [2008-01-15] [2008-02-15] [2008-03-15] [2008-04-15] [2008-05-15] [2008-06-15] [2008-07-15] [2008-08-15] [2008-09-15] [2008-10-15] [2008-11-15] [12] [2008-12-15] [2009-01-15] [2009-02-15] [2009-03-15] [2009-04-15] [2009-05-15] [2009-06-15] [2009-07-15] [2009-08-15] [2009-09-15] [2009-10-15] [23] [2009-11-15] [2009-12-15] [2010-01-15] [2010-02-15] [2010-03-15] [2010-04-15] [2010-05-15] [2010-06-15] [2010-07-15] [2010-08-15] [2010-09-15] [34] [2010-10-15] [2010-11-15] [2010-12-15] In this case the last calculated date is obviously LARGER than the "to" date:> as.Date(test[length(test)]) < "2010-12-13"[1] FALSE This seem to occur also with other parameters:> timeSequence(from = "1999-12-12", to = "2000-06-08", by = "2 months")GMT [1] [1999-12-12] [2000-02-12] [2000-04-12] [2000-06-12]> timeSequence(from = as.Date("1999-12-12"), to = as.Date("2000-06-08"), by= "2 months") GMT [1] [1999-12-12] [2000-02-12] [2000-04-12] [2000-06-12] Am I missing something essential in using timeSequence? Is this behavior wanted (then I don't get why it should...)? Is there a better way to get those dates? Thanks for reading and greetings, Benjamin Benjamin B. Hamburg, Germany
Uwe Ligges
2011-Jan-06 12:57 UTC
[R] Problem with timeSequence {timeDate} - wrong end date
Please report bugs in contributed packages to the corresponding package maintainer. Uwe Ligges On 06.01.2011 10:30, Benjamin B. wrote:> Dear help-list, > > I have a problem with timeSequence {timeDate}. > > When I use it like > >> timeSequence(from = "2008-01-01", to = "2010-12-13", by = "1 month") > GMT > [1] [2008-01-01] [2008-02-01] [2008-03-01] [2008-04-01] [2008-05-01] > [2008-06-01] [2008-07-01] [2008-08-01] [2008-09-01] [2008-10-01] > [2008-11-01] > [12] [2008-12-01] [2009-01-01] [2009-02-01] [2009-03-01] [2009-04-01] > [2009-05-01] [2009-06-01] [2009-07-01] [2009-08-01] [2009-09-01] > [2009-10-01] > [23] [2009-11-01] [2009-12-01] [2010-01-01] [2010-02-01] [2010-03-01] > [2010-04-01] [2010-05-01] [2010-06-01] [2010-07-01] [2010-08-01] > [2010-09-01] > [34] [2010-10-01] [2010-11-01] [2010-12-01] > > The result is as expected: a list of dates with all dates smaller then the > "to" date. > > But somehow it behaves strange when I use it with a different starting date: > >> test<- timeSequence(from = "2008-01-15", to = "2010-12-13", by = "1 > month") > GMT > [1] [2008-01-15] [2008-02-15] [2008-03-15] [2008-04-15] [2008-05-15] > [2008-06-15] [2008-07-15] [2008-08-15] [2008-09-15] [2008-10-15] > [2008-11-15] > [12] [2008-12-15] [2009-01-15] [2009-02-15] [2009-03-15] [2009-04-15] > [2009-05-15] [2009-06-15] [2009-07-15] [2009-08-15] [2009-09-15] > [2009-10-15] > [23] [2009-11-15] [2009-12-15] [2010-01-15] [2010-02-15] [2010-03-15] > [2010-04-15] [2010-05-15] [2010-06-15] [2010-07-15] [2010-08-15] > [2010-09-15] > [34] [2010-10-15] [2010-11-15] [2010-12-15] > > In this case the last calculated date is obviously LARGER than the "to" > date: > >> as.Date(test[length(test)])< "2010-12-13" > [1] FALSE > > This seem to occur also with other parameters: > >> timeSequence(from = "1999-12-12", to = "2000-06-08", by = "2 months") > GMT > [1] [1999-12-12] [2000-02-12] [2000-04-12] [2000-06-12] > >> timeSequence(from = as.Date("1999-12-12"), to = as.Date("2000-06-08"), by > = "2 months") > GMT > [1] [1999-12-12] [2000-02-12] [2000-04-12] [2000-06-12] > > Am I missing something essential in using timeSequence? > Is this behavior wanted (then I don't get why it should...)? > Is there a better way to get those dates? > > Thanks for reading and greetings, > > Benjamin > > > Benjamin B. > Hamburg, Germany > > ______________________________________________ > R-help at 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.
Joshua Wiley
2011-Jan-06 13:27 UTC
[R] Problem with timeSequence {timeDate} - wrong end date
Dear Benjamin, timeSequence() ultimately is relying on seq.POSIXt(). If you look at the last paragraph of the "Details" section in ?seq.POSIXt it seems to basically indicate that using by = "month" just sequences through the months and the day is only changed if it is invalid for a particular month (the relevant lines are around 88--100 in the seq.POSIXt code). It is fairly straightforward ensure the output is less than "to". Here is one option: test <- timeSequence(from = "2008-01-01", to = "2010-12-13", by = "1 month") test <- test[as.Date(test) < as.Date("2010-12-13")] test Cheers, Josh On Thu, Jan 6, 2011 at 1:30 AM, Benjamin B. <benj.bad.ac at googlemail.com> wrote:> Dear help-list, > > I have a problem with timeSequence {timeDate}. > > When I use it like > >> timeSequence(from = "2008-01-01", to = "2010-12-13", by = "1 month") > GMT > ?[1] [2008-01-01] [2008-02-01] [2008-03-01] [2008-04-01] [2008-05-01] > [2008-06-01] [2008-07-01] [2008-08-01] [2008-09-01] [2008-10-01] > [2008-11-01] > [12] [2008-12-01] [2009-01-01] [2009-02-01] [2009-03-01] [2009-04-01] > [2009-05-01] [2009-06-01] [2009-07-01] [2009-08-01] [2009-09-01] > [2009-10-01] > [23] [2009-11-01] [2009-12-01] [2010-01-01] [2010-02-01] [2010-03-01] > [2010-04-01] [2010-05-01] [2010-06-01] [2010-07-01] [2010-08-01] > [2010-09-01] > [34] [2010-10-01] [2010-11-01] [2010-12-01] > > The result is as expected: a list of dates with all dates smaller then the > "to" date. > > But somehow it behaves strange when I use it with a different starting date: > >> test <- timeSequence(from = "2008-01-15", to = "2010-12-13", by = "1 > month") > GMT > ?[1] [2008-01-15] [2008-02-15] [2008-03-15] [2008-04-15] [2008-05-15] > [2008-06-15] [2008-07-15] [2008-08-15] [2008-09-15] [2008-10-15] > [2008-11-15] > [12] [2008-12-15] [2009-01-15] [2009-02-15] [2009-03-15] [2009-04-15] > [2009-05-15] [2009-06-15] [2009-07-15] [2009-08-15] [2009-09-15] > [2009-10-15] > [23] [2009-11-15] [2009-12-15] [2010-01-15] [2010-02-15] [2010-03-15] > [2010-04-15] [2010-05-15] [2010-06-15] [2010-07-15] [2010-08-15] > [2010-09-15] > [34] [2010-10-15] [2010-11-15] [2010-12-15] > > In this case the last calculated date is obviously LARGER than the "to" > date: > >> as.Date(test[length(test)]) < "2010-12-13" > [1] FALSE > > This seem to occur also with other parameters: > >> timeSequence(from = "1999-12-12", to = "2000-06-08", by = "2 months") > GMT > [1] [1999-12-12] [2000-02-12] [2000-04-12] [2000-06-12] > >> timeSequence(from = as.Date("1999-12-12"), to = as.Date("2000-06-08"), by > = "2 months") > GMT > [1] [1999-12-12] [2000-02-12] [2000-04-12] [2000-06-12] > > Am I missing something essential in using timeSequence? > Is this behavior wanted (then I don't get why it should...)? > Is there a better way to get those dates? > > Thanks for reading and greetings, > > Benjamin > > > Benjamin B. > Hamburg, Germany > > ______________________________________________ > R-help at 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. >-- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/
Joshua Wiley
2011-Jan-06 14:47 UTC
[R] Problem with timeSequence {timeDate} - wrong end date
On Thu, Jan 6, 2011 at 5:27 AM, Joshua Wiley <jwiley.psych at gmail.com> wrote:> timeSequence() ultimately is relying on seq.POSIXt(). If you look atMy apologies, I spoke nonsense---timeSequence() does NOT rely on seq.POSIXt(). The timeDate package has its own method defined for seq() which is what gets dispatched. Still, the behavior is similar:> timeSequence(from = "2010-01-15", to = "2010-02-01", by = "1 month")GMT [1] [2010-01-15] [2010-02-15]> > seq.POSIXt(from = as.POSIXct("2010-01-15"),+ to = as.POSIXct("2010-02-01"), by = "1 month") [1] "2010-01-15 PST" "2010-02-15 PST" which is not surprising because the code is responsible for this behavior is similar: ###### seq.timeDate* ###### else if (valid == 6) { if (missing(to)) { mon <- seq.int(r1$mon, by = by, length.out = length.out) } else { to <- as.POSIXlt(to) mon <- seq.int(r1$mon, 12 * (to$year - r1$year) + to$mon, by) } r1$mon <- mon r1$isdst <- -1 res <- as.POSIXct(r1) } ###### seq.POSIXt ###### else if (valid == 6L) { if (missing(to)) { mon <- seq.int(r1$mon, by = by, length.out = length.out) } else { to <- as.POSIXlt(to) mon <- seq.int(r1$mon, 12 * (to$year - r1$year) + to$mon, by) } r1$mon <- mon r1$isdst <- -1 res <- as.POSIXct(r1) } and "res" is the object returned in both cases (I believe). My system: R version 2.12.1 (2010-12-16) Platform: i486-pc-linux-gnu (32-bit) locale: [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C [3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8 [5] LC_MONETARY=C LC_MESSAGES=en_US.UTF-8 [7] LC_PAPER=en_US.UTF-8 LC_NAME=C [9] LC_ADDRESS=C LC_TELEPHONE=C [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] timeDate_2130.91
Martin Maechler
2011-Jan-07 14:59 UTC
[R] Problem with timeSequence {timeDate} - wrong end date
>>>>> Joshua Wiley <jwiley.psych at gmail.com> >>>>> on Thu, 6 Jan 2011 06:47:43 -0800 writes:> On Thu, Jan 6, 2011 at 5:27 AM, Joshua Wiley <jwiley.psych at gmail.com> wrote: >> timeSequence() ultimately is relying on seq.POSIXt(). If you look at > My apologies, I spoke nonsense---timeSequence() does NOT rely on > seq.POSIXt(). The timeDate package has its own method defined for > seq() which is what gets dispatched. Still, the behavior is similar: >> timeSequence(from = "2010-01-15", to = "2010-02-01", by = "1 month") > GMT > [1] [2010-01-15] [2010-02-15] >> >> seq.POSIXt(from = as.POSIXct("2010-01-15"), > + to = as.POSIXct("2010-02-01"), by = "1 month") > [1] "2010-01-15 PST" "2010-02-15 PST" > which is not surprising because the code is responsible for this > behavior is similar: > ###### seq.timeDate* ###### > else if (valid == 6) { > if (missing(to)) { > mon <- seq.int(r1$mon, by = by, length.out = length.out) > } > else { > to <- as.POSIXlt(to) > mon <- seq.int(r1$mon, 12 * (to$year - r1$year) + > to$mon, by) > } > r1$mon <- mon > r1$isdst <- -1 > res <- as.POSIXct(r1) > } > ###### seq.POSIXt ###### > else if (valid == 6L) { > if (missing(to)) { > mon <- seq.int(r1$mon, by = by, length.out = length.out) > } > else { > to <- as.POSIXlt(to) > mon <- seq.int(r1$mon, 12 * (to$year - r1$year) + > to$mon, by) > } > r1$mon <- mon > r1$isdst <- -1 > res <- as.POSIXct(r1) > } > and "res" is the object returned in both cases (I believe). Thank you, Joshua. Note that "R 2.12.1 patched" and "R 2.13.0 unstable" have this fixed (since yesterday) and hence will no longer "overshoot". Also, the next release of the timeDate package will have it fixed. Martin Maechler, ETH Zurich > My system: > R version 2.12.1 (2010-12-16) > Platform: i486-pc-linux-gnu (32-bit) > locale: > [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C > [3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8 > [5] LC_MONETARY=C LC_MESSAGES=en_US.UTF-8 > [7] LC_PAPER=en_US.UTF-8 LC_NAME=C > [9] LC_ADDRESS=C LC_TELEPHONE=C > [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C > attached base packages: > [1] stats graphics grDevices utils datasets methods base > other attached packages: > [1] timeDate_2130.91 > ______________________________________________ > R-help at 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.
Joshua Wiley
2011-Jan-07 15:54 UTC
[R] Problem with timeSequence {timeDate} - wrong end date
Thank you, Martin (and all of R core) for all your work making such wonderful software available. Not only is it practical, using R, going through the source code and the documentation has been a huge learning experience for me. I am very grateful to you and everyone else for being so generous with your time. Josh On Fri, Jan 7, 2011 at 6:59 AM, Martin Maechler <maechler at stat.math.ethz.ch> wrote:> Thank you, Joshua. > > Note that "R 2.12.1 patched" and "R 2.13.0 unstable" > have this fixed (since yesterday) and hence will no longer "overshoot". > > Also, the next release of the ?timeDate ?package will have it > fixed. > > > Martin Maechler, ETH Zurich