I have a dataset with numbers with several significant figures. I need to round to the nearest decimal point up and the nearest decimal point down.
here is an example of the code for reference with some random numbers similar to the ones I'm working with:
x <- c(0.987, 1.125, 0.87359, 1.2)
high_rounded <- round(x, digits = 1)
low_rounded <- high_rounded - 0.1
and then I need to be able to use both the high_rounded and low_rounded variables in further analyses. The way I have the code written right now it will only work when the number is rounded up, but if it needs to be rounded down then it doesn't work. The round() function only works to round a number however it needs to be rounded, but I am not able to specify to round up or down.
I have also tried:
ceiling(x)
But this only rounds up to the nearest integer, and I need the nearest decimal point.
CodePudding user response:
How about ceiling(x*10)/10
... ?
CodePudding user response:
You can use round_any
from the plyr
package
plyr::round_any(x <- c(0.987, 1.125, 0.87359, 1.2), accuracy = 0.1, f = ceiling)
[1] 1.0 1.2 0.9 1.2