Round down to earliest hour or half hour

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Round down to earliest hour or half hour

Schatzi
I have times and would like to round down to the earliest 30 minute increment. For instance, a time of
2011-04-28 09:02:00
(the as.numeric value = 1303999320)
I would like it to be rounded down to:
2011-04-28 09:00:00
(the as.numeric value = 1303999200)

Any ideas of how to do this?
In theory, practice and theory are the same. In practice, they are not - Albert Einstein
Reply | Threaded
Open this post in threaded view
|

Re: Round down to earliest hour or half hour

jannis-2
I would use package chron and trunc()....

One example from the help of trunc.times (modified):

lirary(chron)
tt <- times(c("12:13:14", "15:46:17"))
trunc(tt, times("00:30:00"))


HTH
Jannis

--- Schatzi <[hidden email]> schrieb am Mo, 9.5.2011:

> Von: Schatzi <[hidden email]>
> Betreff: [R] Round down to earliest hour or half hour
> An: [hidden email]
> Datum: Montag, 9. Mai, 2011 14:06 Uhr
> I have times and would like to round
> down to the earliest 30 minute
> increment. For instance, a time of
> 2011-04-28 09:02:00
> (the as.numeric value = 1303999320)
> I would like it to be rounded down to:
> 2011-04-28 09:00:00
> (the as.numeric value = 1303999200)
>
> Any ideas of how to do this?
>
> -----
> In theory, practice and theory are the same. In practice,
> they are not - Albert Einstein
> --
> View this message in context: http://r.789695.n4.nabble.com/Round-down-to-earliest-hour-or-half-hour-tp3509374p3509374.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email]
> 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Round down to earliest hour or half hour

Jeff Newmiller
In reply to this post by Schatzi
One way, assuming your timestamps are POSIXct format:

truncInterval <- function ( x, dx=as.difftime(1,units="days") ) {
xn <- as.numeric( x )
result <- xn - xn %% as.numeric( dx, units="secs" )
class( result ) <- 'POSIXct'
result
}

truncInterval( as.POSIXct(c("2011-04-28 09:20:00", "2011-04-28 09:40:00")), as.difftime(30,units="mins") )

This may not work well if the timestamp granularity is more fine than one second.
---------------------------------------------------------------------------
Jeff Newmiller The ..... ..... Go Live...
DCN:<[hidden email]> 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.

Schatzi <[hidden email]> wrote:

I have times and would like to round down to the earliest 30 minute increment. For instance, a time of 2011-04-28 09:02:00 (the as.numeric value = 1303999320) I would like it to be rounded down to: 2011-04-28 09:00:00 (the as.numeric value = 1303999200) Any ideas of how to do this? ----- In theory, practice and theory are the same. In practice, they are not - Albert Einstein -- View this message in context: http://r.789695.n4.nabble.com/Round-down-to-earliest-hour-or-half-hour-tp3509374p3509374.html Sent from the R help mailing list archive at Nabble.com._____________________________________________
[hidden email] 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.


        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.