[R] round Date object to 10 minutes intervals?

Prof Brian Ripley ripley at stats.ox.ac.uk
Mon Dec 12 12:29:26 CET 2011


You haven't specified how you want 00:05 rounded (it is neither > nor 
< 5).  But here's one way

t2 <- strptime(timeStamp, format="%d.%m.%Y %H:%M")
t2$min <- round(t2$min, -1)
> format(t2, "%d.%m.%Y %H:%M")
[1] "31.03.2011 09:30" "31.03.2011 09:40" "31.03.2011 10:00" "31.03.2011 10:10"
[5] "31.03.2011 10:30" "31.03.2011 10:40" "31.03.2011 10:50" "01.04.2011 00:00"

That will round 5 to even.  You may need to consider things like
t2$min <- 10*floor((t2$min + 5)/10)
t2$min <- 10*floor((t2$min + 4)/10)



On Mon, 12 Dec 2011, Katharina May wrote:

> Hello *,
>
> I'm trying to round time stamps, consisting of a date and time part, to 10
> minutes intervals
> The timestamps have the following format: %d.%m.%Y %H:%M
>
> *timeStamp <- c("31.03.2011 09:30", "31.03.2011 09:41", "31.03.2011 10:04",
> "31.03.2011 10:10", "31.03.2011 10:28", "31.03.2011 10:35", "31.03.2011
> 10:49", "31.03.2011 23:59")*
>
> should look like this:
> *timeStamp2 <- c("31.03.2011 09:30", "31.03.2011 09:40", "31.03.2011
> 10:00", "31.03.2011 10:10", "31.03.2011 10:30", "31.03.2011 10:40",
> "31.03.2011 10:55", "01.04.2011 00:00")*
>
> I've found this topic on the web:
> http://stats.stackexchange.com/questions/5305/how-to-re-sample-an-xts-time-series-in-r
>
> library(xts)
> align.time(as.POSIXct(timeStamp, format="%d.%m.%Y %H:%M"), 10 * 60)
>
> which uses the align.time function from the library xts BUT as far as I can
> see only supports either all timestamps being either  rounded up or down
> (not both, depending on the minutes 0-9 being either > or < 5 line the way
> I need it)
>
> As I'm really struggling with this topic, any help and hint is  awarded
> with greatest thankfulness from my side?
>
> Best regards, Katharina
>
> 	[[alternative HTML version deleted]]
>
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-help mailing list