Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Round-up and round-down R

Tags:

rounding

r

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

like image 679
hn.phuong Avatar asked Nov 24 '25 07:11

hn.phuong


1 Answers

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))
like image 172
AK88 Avatar answered Nov 26 '25 23:11

AK88



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!