0

Is there a way in R that rounds up integer numbers to the next 10? For example all values from 0 to 9 will be converted to 10. 10 of course remains 10. Then all values from 11 to 19 are converted to 20 etc. I have founded how to round up to tne nearest 10 with: round_to <- function(x, to = 10) round(x/to)*to

firmo23
  • 6,014
  • 1
  • 20
  • 70

1 Answers1

1

I couldn't find an actual duplicate answer on Stack Overflow, so I am posting the following formula:

next_ten <- function(x) { 10*ceiling(x/10) }
v <- c(1, 5, 10, 11, 20, 29, 30, 31)
v
next_ten(v)

[1]  1  5 10 11 20 29 30 31
[1] 10 10 10 20 20 30 30 40

This tricks works by first finding how many tens, or decimal fractions of tens, there are (ceiling(x/10)). Then, we multiply by 10, to get the effective ceiling you want.

Tim Biegeleisen
  • 451,927
  • 24
  • 239
  • 318