Availability of NA level for missing values ββwith cut function from R
Cut function in R omits NA. But I want to have a level for missing values. Here is my MWE.
set.seed(12345)
Y <- c(rnorm(n = 50, mean = 500, sd = 1), NA)
Y1 <- cut(log(Y), 5)
Labs <- levels(Y1)
Labs
[1] "(6.21,6.212]" "(6.212,6.213]" "(6.213,6.215]" "(6.215,6.217]" "(6.217,6.219]"
Desired result
[1] "(6.21,6.212]" "(6.212,6.213]" "(6.213,6.215]" "(6.215,6.217]" "(6.217,6.219]" "NA"
+3
source to share
2 answers
you can use addNA
Labs <- levels(addNA(Y1))
Labs
#[1] "(6.21,6.212]" "(6.212,6.213]" "(6.213,6.215]" "(6.215,6.217]"
#[5] "(6.217,6.219]" NA
You had an "NA" character in the expected output. But, I think it's better to have a real NA, since it can be removed / replaced withis.na
is.na(Labs)
#[1] FALSE FALSE FALSE FALSE FALSE TRUE
+8
source to share
Changing the original third line of the MWE to the following NA stores (actually) in Y1 and not to external vector Labs. This clears up analytical tasks such as creating tables or building models. NA is also still recognized by is.na.
Y1 <- coefficient (cut (log (Y), 5), exclude = NULL)
is.na (levels (Y1)) [1] FALSE FALSE FALSE FALSE FALSE TRUE
0
source to share