How to round up to the nearest 10 (or 100 or X)?

后端 未结 11 1852
执笔经年
执笔经年 2020-11-27 12:20

I am writing a function to plot data. I would like to specify a nice round number for the y-axis max that is greater than the max of the dataset.

Specif

相关标签:
11条回答
  • 2020-11-27 12:53

    How about:

    roundUp <- function(x,to=10)
    {
      to*(x%/%to + as.logical(x%%to))
    }
    

    Which gives:

    > roundUp(c(4,6.1,30.1,100.1))
    [1]  10  10  40 110
    > roundUp(4,5)
    [1] 5
    > roundUp(12,7)
    [1] 14
    
    0 讨论(0)
  • 2020-11-27 12:54

    Regarding the rounding up to the multiplicity of an arbitrary number, e.g. 10, here is a simple alternative to James's answer.

    It works for any real number being rounded up (from) and any real positive number rounded up to (to):

    > RoundUp <- function(from,to) ceiling(from/to)*to
    

    Example:

    > RoundUp(-11,10)
    [1] -10
    > RoundUp(-0.1,10)
    [1] 0
    > RoundUp(0,10)
    [1] 0
    > RoundUp(8.9,10)
    [1] 10
    > RoundUp(135,10)
    [1] 140
    
    > RoundUp(from=c(1.3,2.4,5.6),to=1.1)  
    [1] 2.2 3.3 6.6
    
    0 讨论(0)
  • 2020-11-27 12:55

    You will find an upgraded version of Tommy's answer that takes into account several cases:

    • Choosing between lower or higher bound
    • Taking into account negative and zero values
    • two different nice scale in case you want the function to round differently small and big numbers. Example: 4 would be rounded at 0 while 400 would be rounded at 400.

    Below the code :

    round.up.nice <- function(x, lower_bound = TRUE, nice_small=c(0,5,10), nice_big=c(1,2,3,4,5,6,7,8,9,10)) {
      if (abs(x) > 100) {
        nice = nice_big
      } else {
        nice = nice_small
      }
      if (lower_bound == TRUE) {
        if (x > 0) {
          return(10^floor(log10(x)) * nice[[max(which(x >= 10^floor(log10(x)) * nice))[[1]]]])
        } else if (x < 0) {
          return(- 10^floor(log10(-x)) * nice[[min(which(-x <= 10^floor(log10(-x)) * nice))[[1]]]])
        } else {
          return(0)
        }
      } else {
        if (x > 0) {
          return(10^floor(log10(x)) * nice[[min(which(x <= 10^floor(log10(x)) * nice))[[1]]]])
        } else if (x < 0) {
          return(- 10^floor(log10(-x)) * nice[[max(which(-x >= 10^floor(log10(-x)) * nice))[[1]]]])
        } else {
          return(0)
        }
      }
    }
    
    0 讨论(0)
  • 2020-11-27 12:56

    The plyr library has a function round_any that is pretty generic to do all kinds of rounding. For example

    library(plyr)
    round_any(132.1, 10)               # returns 130
    round_any(132.1, 10, f = ceiling)  # returns 140
    round_any(132.1, 5, f = ceiling)   # returns 135
    
    0 讨论(0)
  • 2020-11-27 13:03

    I tried this without using any external library or cryptic features and it works!

    Hope it helps someone.

    ceil <- function(val, multiple){
      div = val/multiple
      int_div = as.integer(div)
      return (int_div * multiple + ceiling(div - int_div) * multiple)
    }
    
    > ceil(2.1, 2.2)
    [1] 2.2
    > ceil(3, 2.2)
    [1] 4.4
    > ceil(5, 10)
    [1] 10
    > ceil(0, 10)
    [1] 0
    
    0 讨论(0)
  • 2020-11-27 13:06

    If you add a negative number to the digits-argument of round(), R will round it to the multiples of 10, 100 etc.

        round(9, digits = -1) 
        [1] 10    
        round(89, digits = -1) 
        [1] 90
        round(89, digits = -2) 
        [1] 100
    
    0 讨论(0)
提交回复
热议问题