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

后端 未结 11 1950
执笔经年
执笔经年 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
    

提交回复
热议问题