What's a good way to cut()
a quantiative variable into levels, including a final level dedicated to NAs?
I'd prefer something like the .missing
parameter that tidyverse functions commonly offer
(e.g., dplyr::recode()
& dplyr::if_else()
).
If the input is w
and this hypothetical function is named cut_with_nas
, then the following code
w <- c(0L, NA_integer_, 22:25, NA_integer_, 40)
cut_with_nas(w, breaks=2)
would produce this desired output:
[1] (-0.04,20] Unknown (20,40] (20,40] (20,40] (20,40] Unknown (20,40]
Levels: (-0.04,20] (20,40] Unknown
I'm posting a function below that accomplishes this, but I was hoping there's a more concise solution, or at least a tested function already existing in a package.