It might seem silly but I have not been successful in trying to round my data as I wanted. This is an example of my array:
a<-c(-0.5:30,by=5)
What I want is: for min(a) to round-down to the next number to multiples of 10 but the max(a) to round-up.
This case:
min(a)=-0.5 and I want it round down to -10
max(a)=29.5 and I want to round up (to 30 or 40).
I have spent time to think and search for it but have not found anything. Any help would be highly appreciated.
Regards, Phuong
a<- seq(from = -0.5, to = 30, by = 5)
For rounding up max:
roundUp <- function(x,to=10)
{
to*(x%/%to + as.logical(x%%to))
}
roundUp(max(a))
For rounding down min:
roundDw <- function(x,to=-10)
{
to*(x%/%to + as.logical(x%%to))
}
roundDw(min(a))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With