converting a time to nearest half-hour
By entering "trunc.POSIXt" at the R commandline, you can see the standard truncate implementation. Riffing on this,
roundhalfhour <- function( x ) {
x <- as.POSIXlt( x + as.difftime( 15, units="mins" ) )
x$sec <- 0
x$min <- 30*(x$min %/% 30)
as.POSIXct(x)
}
The as.double approach ought to work also, but rounding error might be a problem.
"David Winsemius" <dwinsemius at comcast.net> wrote:
On Jul 23, 2010, at 11:35 AM, David Winsemius wrote:
On Jul 23, 2010, at 11:20 AM, <Murali.Menon at avivainvestors.com> <Murali.Menon at avivainvestors.com
wrote:
Hi folks, I've got a POSIXct datum as follows:
Sys.time()
[1] "2010-07-23 11:29:59 BST" I want to convert this to the nearest half-hour, i.e., to "2010-07-23 11:30:00 BST" (If the time were "11:59:ss", I want to convert to "12:00:00"). How to achieve this?
Couldn't you just coerce to numeric, divide by 60(sec)*30(half-hour minutes), round to integer, multiply by 60*30, coerce to POSIXct?
When I tried my method I see that one also needs to add or subtract the proper number of seconds from Universal Time to get the output formatting correct. (Probably demonstrates that I do not have the proper understanding of the right place to employ a TZ specification.). David Winsemius, MD West Hartford, CT
______________________________________________ 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.
---------------------------------------------------------------------------
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
---------------------------------------------------------------------------
Sent from my phone. Please excuse my brevity.