Pelle Pelle - 1 month ago 7
R Question

R: round with increments larger 1

How to round by increment larger than 1? Acctualy, I need the next "ceiling" in steps of 5. Example:

ceiling(c(2.5, 6, 9.9, 10, 11, 14, 15), inc = 5) #not working
> 5 10 10 15 15 15 20


I tried
ceiling()
,
round()
and
signif()
but I could not figure it out. It's probably a repetition but my research always leads me to increments smaler than 1.

Thanks in advanced!

Answer

This should work for you

a=c(2.5, 6, 9.9, 10, 11, 14, 15)
fiver=function(x)
{
  multiplier=floor(x/5)+1
  return(5*(multiplier))
}

fiver(a)

[1] 5 10 10 15 15 15 20