[R] converting a time to nearest half-hour

Murali.Menon at avivainvestors.com Murali.Menon at avivainvestors.com
Fri Jul 23 18:35:18 CEST 2010


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 didn't make any difference. So there's something else that I'm missing. I'll take a look at your various approaches, and get back to you.
Cheers,
Murali

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of David Winsemius
Sent: 23 July 2010 17:15
To: stephen sefick
Cc: r-help at r-project.org
Subject: Re: [R] 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), function(x) mean(x,
> na.rm=TRUE))
> }

This "works" for producing 10 half-hour intervals of EDT times:

as.POSIXct(60*30*( round( as.numeric( Sys.time()+
                       60*30*(1:10))/  # the sequence creation
                       (60*30))) -   # divide prior to rounding
                       5*60*60,    # the TZ offset
                       origin="1970-01-01" )
  [1] "2010-07-23 12:30:00 EDT" "2010-07-23 13:00:00 EDT"
  [3] "2010-07-23 13:30:00 EDT" "2010-07-23 14:00:00 EDT"
  [5] "2010-07-23 14:30:00 EDT" "2010-07-23 15:00:00 EDT"
  [7] "2010-07-23 15:30:00 EDT" "2010-07-23 16:00:00 EDT"
  [9] "2010-07-23 16:30:00 EDT" "2010-07-23 17:00:00 EDT"

>
> hth
>
> Stephen Sefick
>
> On Fri, Jul 23, 2010 at 11:00 AM, 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.
>>
>
>
>
> --
> Stephen Sefick
> ____________________________________
> | Auburn University                                   |
> | Department of Biological Sciences           |
> | 331 Funchess Hall                                  |
> | Auburn, Alabama                                   |
> | 36849                                                    |
> |___________________________________|
> | sas0025 at auburn.edu                             |
> | http://www.auburn.edu/~sas0025             |
> |___________________________________|
>
> Let's not spend our time and resources thinking about things that are 
> so little or so large that all they really do for us is puff us up and 
> make us feel like gods.  We are mammals, and have not exhausted the 
> annoying little problems of being mammals.
>
>                                 -K. Mullis

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.



More information about the R-help mailing list