0

I'm stuck in the unenviable position of attempting to replicate a MS Access function called Cint inside of R. The way Cint works is that anything less than or equal to 1.5 is rounded down.

  • Cint(1.5) = 1
  • Cint(1.5000000001) = 2
  • Cint(1.4999999) =1

https://www.techonthenet.com/access/functions/datatype/cint.php

More sensibly base R rounds 1.5 up to 2.

  • round(1.5) = 2

How do I coerce R to round data following Cint's rules? In this case being consistent is preferable to accuracy.

Big_Red
  • 31
  • 4
  • https://stackoverflow.com/questions/34845236/round-but-5-should-be-floored – rawr Mar 18 '21 at 22:39
  • Your description of CInt seems to contradict the [official docs](https://support.microsoft.com/en-us/office/type-conversion-functions-8ebb0e94-2d43-4975-bb13-87ac8d1a2202), which say "When the fractional part is exactly 0.5, CInt and CLng always round it to the nearest even number. For example, 0.5 rounds to 0, and 1.5 rounds to 2.". Any idea where the discrepancy comes from? – Mark Dickinson Mar 20 '21 at 12:02

1 Answers1

0

You can define your own CInt like below

CInt <- function(x) floor(x) + ifelse(x %% 1 > 0.5, 1, 0)
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81