I need to categorise a numeric vector. I wanted to use the function cut()
and was happy with the result. However, digging in the result, I found a miscategorised item. Minimal working example below. Questions step by step in the code:
# What can I change in `classify()` to avoid divergent classification result
# between `(18 - 1.8)/18 * 100` and 90? (see below)
classify <- function(x) cut(x, breaks = c(-Inf, 90, Inf), right = FALSE)
# Lets consider a:
a <- (18 - 1.8)/18 * 100
a
#> [1] 90
# Why `a` is not equal to `90`?
a == 90
#> [1] FALSE
# Why the classification results are different?
classify(a)
#> [1] [-Inf,90)
#> Levels: [-Inf,90) [90, Inf)
classify(90)
#> [1] [90, Inf)
#> Levels: [-Inf,90) [90, Inf)
Created on 2021-05-28 by the reprex package (v0.2.1)