Can case_when()
in dplyr
return combination of values NA
and does not NA
?
When I ask you to return NA
in response to one operator, but the value is not NA
in response to another operator, it gives an evaluation error:
For example, I want 1
for all values cyl >= 6
and NA
for valuescyl < 6
> library("dplyr")
> mtcars %>% mutate(thing = case_when(cyl >= 6 ~ 1, cyl < 6 ~ NA ))
Error in mutate_impl file (.data, dots): evaluation error: it must be double, illogical.
One, both statements are fined.
This problem is absent if a return is requested for all NSs, but not a mixture NA
or NA
s.
For example: Return NA
for all valuescyl >= 6
> mtcars %>% mutate(thing = case_when(cyl >= 6 ~ NA))
cyl thing
1 6 NA
2 6 NA
3 4 NA
Looks nice.
> 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 ))
mutate_impl (.data, dots): : double, .
.
NB: mtcars[1:3,]
%>% select(cyl, thing)
.