[R] converting a time to nearest half-hour

2010-07-23 Thread Murali.Menon
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? Thanks, Murali

Re: [R] converting a time to nearest half-hour

2010-07-23 Thread David Winsemius
On Jul 23, 2010, at 11:20 AM, murali.me...@avivainvestors.com murali.me...@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

Re: [R] converting a time to nearest half-hour

2010-07-23 Thread David Winsemius
On Jul 23, 2010, at 11:35 AM, David Winsemius wrote: On Jul 23, 2010, at 11:20 AM, murali.me...@avivainvestors.com murali.me...@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

Re: [R] converting a time to nearest half-hour

2010-07-23 Thread stephen sefick
If you have a zoo series this should work. If it doesn't then please tell me because I think it works. snap2min - function(zoo, min=00:15:00){ min15 - times(min) a - aggregate(zoo, trunc(time(zoo), min15), function(x) mean(x, na.rm=TRUE)) } hth Stephen Sefick On Fri, Jul 23, 2010 at 11:00 AM,

Re: [R] converting a time to nearest half-hour

2010-07-23 Thread David Winsemius
On Jul 23, 2010, at 12:04 PM, stephen sefick wrote: If you have a zoo series this should work. If it doesn't then please tell me because I think it works. snap2min - function(zoo, min=00:15:00){ min15 - times(min) a - aggregate(zoo, trunc(time(zoo), min15), function(x) mean(x, na.rm=TRUE))

Re: [R] converting a time to nearest half-hour

2010-07-23 Thread Jeff Newmiller
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

Re: [R] converting a time to nearest half-hour

2010-07-23 Thread Jeff Newmiller
This truncates to quarter-hour rather than rounding to half-hour. stephen sefick ssef...@gmail.com wrote: If you have a zoo series this should work. If it doesn't then please tell me because I think it works. snap2min - function(zoo, min=00:15:00){ min15 - times(min) a - aggregate(zoo,

Re: [R] converting a time to nearest half-hour

2010-07-23 Thread Allan Engelhardt
The arithmetic that David describes should work fine (POSIXct is internally in UTC) unless you are in the Chatham Islands (which has a UTC+12:45 time zone [1]) or Nepal (UTC+05:45 [2]) or some such place with a funny idea about when the 1/2 hour mark is. The formatting of the output may be

Re: [R] converting a time to nearest half-hour

2010-07-23 Thread Murali.Menon
] converting a time to nearest half-hour On Jul 23, 2010, at 12:04 PM, stephen sefick wrote: If you have a zoo series this should work. If it doesn't then please tell me because I think it works. snap2min - function(zoo, min=00:15:00){ min15 - times(min) a - aggregate(zoo, trunc(time(zoo), min15

Re: [R] converting a time to nearest half-hour

2010-07-23 Thread Gabor Grothendieck
On Fri, Jul 23, 2010 at 12:35 PM, murali.me...@avivainvestors.com wrote: David, Stephen, You're right - it's the time zone conventions that threw me as well. I tried those round() operations earlier, but inevitably ended up being either an hour behind. Even when I specified my time zone, it