0

I was always taught that for numbers 1,2,3 and 4 you round DOWN and for 5,6,7,8 and 9 you round UP. So can someone please explain to me why when using round or signif in R on 6.5 it rounds it down to 6??

round(6.5)
[1] 6
signif(6.5)
[1] 6

i need my values to round up when it gives a .5 number. Can someone please tell me how to do this?

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453

1 Answers1

1

I did this:

round2 <- function(x) {
  index <- gregexpr(pattern='\\.', as.character(x)  ) #find the index of the dot
  if ( substr(as.character(x), index[[1]]+1, index[[1]]+1 ) =='5' ) round(x+0.1) else round(x) #check if the first decimal is a 5
}

> round2(5.5)
[1] 6
> round2(255.5)
[1] 256
> round2(-55.5)
[1] -56

This seems to be working in any case.

LyzandeR
  • 37,047
  • 12
  • 77
  • 87