Skip to content
Prev 279021 / 398506 Next

round to specific intervals

Thanks for all the response,

I manage to write a small function to complete what I want based on the 
wonderful helps.

iround <- function(x, interval){
     ## Round numbers to desired interval
     ##
     ## Args:
     ##   x:        Numeric vector to be rounded
     ##   interval: The interval the values should be rounded towards.
     ## Retunrs:
     ##   a numeric vector with x rounded to the desired interval.
     ##
     interval[ifelse(x < min(interval), 1, findInterval(x, interval))]
}

iround(0:100, c(1, 5, 10, 20, 50))

Cheers, and thanks for all the help again.
On 1/12/2011 5:20 p.m., Duncan Murdoch wrote: