[R] seq() problem with chron
Jeff Newmiller
jdnewm|| @end|ng |rom dcn@d@v|@@c@@u@
Thu Sep 6 09:54:25 CEST 2018
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 using pnnl.gov
>
> ______________________________________________
> R-help using 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 using 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
More information about the R-help
mailing list