Hi, I encountered the problem below where the last value in the chron vector created with seq() should have a time of 15:30, but instead has 15:15. What causes this and how can I make sure that the last value in the chron vector is the same as the "to" value in seq()? library(chron) dt1 <- chron("02/20/13", "00:00:00") dt2 <- chron("07/03/18", "15:30:00") dt <- seq(from=dt1, to=dt2, by=1/(24*4)) dt[length(dt)] #[1] (07/03/18 15:15:00) Thanks, Scott Waichler Pacific Northwest National Laboratory scott.waichler at pnnl.gov
On 9/5/2018 10:00 PM, Waichler, Scott R wrote:> Hi, > > I encountered the problem below where the last value in the chron vector created with seq() should have a time of 15:30, but instead has 15:15. What causes this and how can I make sure that the last value in the chron vector is the same as the "to" value in seq()? > > library(chron) > dt1 <- chron("02/20/13", "00:00:00") > dt2 <- chron("07/03/18", "15:30:00") > dt <- seq(from=dt1, to=dt2, by=1/(24*4)) > dt[length(dt)] > #[1] (07/03/18 15:15:00) > > Thanks, > Scott Waichler > Pacific Northwest National Laboratory > scott.waichler at pnnl.gov > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. >This is not a chron problem, it is a floating-point arithmetic problem (basically, FAQ 7.31). You are adding incrementing by 1/96, which can't be represent exactly in binary representation. So, when you expected that you should get a time of 15:30, it is slightly larger and the sequence is stopped at 15:15. You could change dt2 to be chron("07/03/18", "15:31:00"). Or or you could use POSIX datetimes with something like the following, where the increment 900 is the number of seconds in 15 minutes. dt1 <- strptime("02/20/13 00:00:00", "%m/%d/%y %H:%M:%S") dt2 <- strptime("07/03/18 15:30:00", "%m/%d/%y %H:%M:%S") dt <- seq(from=dt1, to=dt2, by=900) dt[length(dt)] There might also be some useful functions in the lubridate package. Hope this is helpful, Dan -- Daniel Nordlund Port Townsend, WA USA
See FAQ 7.31... chron uses floating point representation, and there is some error accumulating. I also think there may be at least one bug in chron::seq.dates(), but I think POSIXct is significantly better than chron anywway so I don't intend to debug chron. ############################# # with chron, avoiding repeated addition of fractional days library(chron) dt1 <- chron("02/20/13", "00:00:00") dt2 <- chron("07/03/18", "15:30:00") n <- round( 24*4*as.numeric( dt2 - dt1 ) ) length(dt) #> [1] 1 dt <- dt1 + seq( 0, n ) / 24 / 4 dt[length(dt)] #> [1] (07/03/18 15:30:00) # with POSIXct Sys.setenv( TZ = "GMT" ) DT1 <- as.POSIXct( "02/20/2013 00:00:00", format = "%m/%d/%Y %H:%M:%S" ) DT2 <- as.POSIXct( "07/03/2018 15:30:00", format = "%m/%d/%Y %H:%M:%S" ) # POSIXct is represented as seconds, so no fractions are used DT <- seq( DT1, DT2, by = as.difftime( 15, units="mins" ) ) DT[length(DT)] #> [1] "2018-07-03 15:30:00 GMT" #' Created on 2018-09-06 by the [reprex package](http://reprex.tidyverse.org) (v0.2.0). ############################# On Thu, 6 Sep 2018, Waichler, Scott R wrote:> Hi, > > I encountered the problem below where the last value in the chron vector created with seq() should have a time of 15:30, but instead has 15:15. What causes this and how can I make sure that the last value in the chron vector is the same as the "to" value in seq()? > > library(chron) > dt1 <- chron("02/20/13", "00:00:00") > dt2 <- chron("07/03/18", "15:30:00") > dt <- seq(from=dt1, to=dt2, by=1/(24*4)) > dt[length(dt)] > #[1] (07/03/18 15:15:00) > > Thanks, > Scott Waichler > Pacific Northwest National Laboratory > scott.waichler at pnnl.gov > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. >--------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k