R round to nearest .5 or .1

后端 未结 4 469
忘了有多久
忘了有多久 2020-11-27 15:44

I have a data set of stock prices that have already been rounded to 2 decimal places (1234.56). I am now trying to round to a specific value which is different

4条回答
  •  不知归路
    2020-11-27 16:12

    Like what JoshO'Brien said in the comments: round_any in the package plyr works very well!

    > library(plyr)
    > stocks <- c(123.45, 155.03, 138.24, 129.94)
    > round_any(stocks,0.1)
    [1] 123.4 155.0 138.2 129.9
    > 
    > round_any(stocks,0.5)
    [1] 123.5 155.0 138.0 130.0
    > 
    > round_any(stocks,0.1,f = ceiling)
    [1] 123.5 155.1 138.3 130.0
    > 
    > round_any(stocks,0.5,f = floor)
    [1] 123.0 155.0 138.0 129.5
    

    Read more here: https://www.rdocumentation.org/packages/plyr/versions/1.8.4/topics/round_any

提交回复
热议问题