Can case_when()
in dplyr
return a mix of NA
and non-NA
values?
When I ask it to return NA
in response to one statement, but an non-NA
value in response to another statement, it throws an evaluation error:
E.g, I want 1
for all values of cyl >= 6
, and NA
for values of cyl < 6
> library("dplyr")
> mtcars %>% mutate(thing = case_when(cyl >= 6 ~ 1, cyl < 6 ~ NA ))
Error in mutate_impl(.data, dots) : Evaluation error: must be type double, not logical.
Alone, both of the statements evaluate fine.
This problem isn't present if asking for all NAs to be returned, but not a mixture of NA
s and non-NA
s.
E.g.: Return NA
for all values of cyl >= 6
> mtcars %>% mutate(thing = case_when(cyl >= 6 ~ NA))
cyl thing
1 6 NA
2 6 NA
3 4 NA
Looks good.
> mtcars %>% mutate(thing = case_when(cyl >= 6 ~ NA, cyl < 6 ~ NA ))
cyl thing
1 6 NA
2 6 NA
3 4 NA
Cool.
> mtcars[1:3,] %>% mutate(thing = case_when(cyl == 6 ~ 1, cyl < 6 ~ NA, cyl > 6 ~ NA ))
Error in mutate_impl(.data, dots) : Evaluation error: must be type double, not logical.
Not cool.
NB: for clarity returned items in the examples are all from mtcars[1:3,]
with %>% select(cyl, thing)
at the end of the expression.
Here is the problem with class
. We need NA_real
to match the numeric type
mtcars %>%
mutate(thing = case_when(cyl >= 6 ~ 1,
cyl < 6 ~ NA_real_ ))
Also, for the second case
mtcars[1:3,] %>%
mutate(thing = case_when(cyl == 6 ~ 1,
cyl < 6 ~ NA_real_,
cyl > 6 ~ NA_real_ )) %>%
select(cyl, thing)
# cyl thing
# 6 1
# 6 1
# 4 NA
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With