[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
__
R-help@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.


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 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?


_
David Winsemius, MD
West Hartford, CT

__
R-help@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.


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 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@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.


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, David Winsemius
dwinsem...@comcast.net wrote:

 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 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@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                                                    |
|___|
| sas0...@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

__
R-help@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.


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))

}


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
dwinsem...@comcast.net wrote:


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 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@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|
|___|
| sas0...@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@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.


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 also, but rounding error might be a 
problem.

David Winsemius dwinsem...@comcast.net wrote:


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 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@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 NewmillerThe .   .  Go Live...
DCN:jdnew...@dcn.davis.ca.usBasics: ##.#.   ##.#.  Live Go...
  Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/BatteriesO.O#.   #.O#.  with
/Software/Embedded Controllers)   .OO#.   .OO#.  rocks...1k
---
Sent from my phone. Please excuse my brevity.

__
R-help@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.


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, trunc(time(zoo), min15), function(x) mean(x, na.rm=TRUE))
}

hth

Stephen Sefick

On Fri, Jul 23, 2010 at 11:00 AM, David Winsemius
dwinsem...@comcast.net wrote:

 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 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@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                                                    |
|___|
| sas0...@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

__
R-help@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 NewmillerThe .   .  Go Live...
DCN:jdnew...@dcn.davis.ca.usBasics: ##.#.   ##.#.  Live Go...
  Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/BatteriesO.O#.   #.O#.  with
/Software/Embedded Controllers)   .OO#.   .OO#.  rocks...1k
---
Sent from my phone. Please excuse my brevity.

__
R-help@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.


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 tricky, if the original poster 
really want the (past/future) time zone as opposed to the current one, 
which is what his text could be read to suggest.


Good test cases would be 2010-03-28 01:46:00 GMT which should 
(presumably) round to 2010-03-28 03:00:00 BST and 2010-10-31 02:46 
BST which probably rounds to 2010-10-31 02:00 GMT.  I can't quite get 
it to work in R, but it should be possible



Allan

[1] https://secure.wikimedia.org/wikipedia/en/wiki/UTC%2B12:45
[2] https://secure.wikimedia.org/wikipedia/en/wiki/UTC%2B05:45

On 23/07/10 16:35, 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 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?


_
David Winsemius, MD
West Hartford, CT



__
R-help@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.


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

2010-07-23 Thread Murali.Menon
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-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On 
Behalf Of David Winsemius
Sent: 23 July 2010 17:15
To: stephen sefick
Cc: r-help@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 
 dwinsem...@comcast.net wrote:

 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 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@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|
 |___|
 | sas0...@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@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.

__
R-help@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.


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 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.

Does your problem really involve time zones?  If not you would likely
be best off using a datetime class that has no time zone so you don't
run into this problem in the first place.  Also its particularly easy
with chron due to the availability of trunc.times() :

   library(chron)
   now - as.chron(format(Sys.time()))

   trunc(now + as.numeric(times(00:15:00)), 00:30:00)

With POSIXct you could muck with the internals like this (which uses
the fact that there are 1800 seconds in an half hour):

   x - Sys.time()
   structure(1800 * (as.numeric(x + 900) %/% 1800), class = class(x))

See the article in R News 4/1 for more.

__
R-help@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.