Search code examples
rroundingfloor

Round Down to the Nearest 10


So I have a column of values ranging from 10 to 100 and I want all of them to be rounded to the nearest 10. The trick is I want it to always round down. For example, 55 would become 50 not 60. I imagine floor would be implemented for this but when I tried floor, it only returned the same value unaltered.

x
10
15
20
27
30
34

etc...

What I want:

x
10
10
20
20
30
30

What I tried:

data$x <- floor(data$x)

This only gave me the exact same values.


Solution

  • Since floor(x) gets the smallest integer y not greater than x, you can divide all the values in x by ten, get the floor, then multiply back by ten; i.e., you can use floor(x/10) * 10

    x <- c(10,
           15,
           20,
           27,
           30,
           34)
    floor(x/10) * 10
    # [1] 10 10 20 20 30 30