[R] Help with Time

Boris Steipe boris.steipe at utoronto.ca
Sat Nov 22 00:36:38 CET 2014


Perhaps this ...

roundToHalf <- function(t) { 
	t <- as.POSIXlt(t)
	if (t$min < 15) {
		t$min <- 0
	} 
	else if (t$min < 45) {
		t$min <- 30		
	}
	else {
		t$min <- 0
		t$hour <- t$hour + 1
	}
	return(t)
}

# check
for (i in 1:20) {
	a <- Sys.time() + (i*490)  # semi arbitrary intervals
	b <- roundToHalf(a)
	print(paste(a, "  ", b))
}


B.


On Nov 21, 2014, at 5:55 PM, Raghuraman Ramachandran <optionsraghu at gmail.com> wrote:

> Sorry I forgot to mention it clearly. I like to round it to the
> nearest 30th minute that is past. So 12:28:59 will be again 12:00:00
> and
> 12:59:59 will be 12:30:00 etc. Apologies for the lack of clarity in
> the beginning.
> 
> Many thanks
> Raghu
> 
> On Fri, Nov 21, 2014 at 10:52 PM, Raghuraman Ramachandran
> <optionsraghu at gmail.com> wrote:
>> Dear guRus
>> 
>> How can I round of time in R to the nearest 30th minute please?
>> 
>> For example suppose if
>>> Sys.time()
>> [1] "2014-11-21 22:49:05.59042 GMT"
>> then I would like a function that outputs 22:30:00.
>> 
>> if Sys.time is 12:13:22 then I would like to get 12:00:00 etc.
>> 
>> Any help would be appreciated.
>> 
>> Many thanks and regards,
>> Raghu
> 
> ______________________________________________
> 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