Does anyone know why dplyr::case_when()
produces the error in the following code?
tibble(tmp1 = sample(c(T, F), size = 32, replace = T),
tmp2 = sample(c(T, F), size = 32, replace = T),
tmp3 = sample(c(T, F), size = 32, replace = T)) %>%
mutate(tmp = apply(cbind(tmp1, tmp2, tmp3), 1, function(x) {
case_when(
all(x == F) ~ "N",
any(x == T) ~ "Y"
)
}))
Error in mutate_impl(.data, dots) :
Evaluation error: object 'x' not found.
I am using R 3.4.3 with dplyr 0.7.4 on Ubuntu 16.04.
The error message is quite confusing, since the following code works fine, which indicates that x
is not missing:
tibble(tmp1 = sample(c(T, F), size = 32, replace = T),
tmp2 = sample(c(T, F), size = 32, replace = T),
tmp3 = sample(c(T, F), size = 32, replace = T)) %>%
mutate(tmp = apply(cbind(tmp1, tmp2, tmp3), 1, function(x) {
if (all(x == F)) {
"N"
} else if(any(x == T)) {
"Y"
}
}))
Just for reference, the following code also works fine:
cbind(tmp1 = sample(c(T, F), size = 32, replace = T),
tmp2 = sample(c(T, F), size = 32, replace = T),
tmp3 = sample(c(T, F), size = 32, replace = T)) %>%
apply(1, function(x) {
case_when(
all(x == F) ~ "N",
any(x == T) ~ "Y"
)
})
case_when()
(which is more convenient than if-else) code produces the error? - NickZengcase_when
doesn't work? ("The issue iscase_when
does not do row-wise operation.") BTW, I don't get an error, but all the entries areNA
. - Maurits Everscase_when
inapply
, see the updated example in the question. I think @www did not mention why "object x" couldn't be found. I am using Ubuntu 16.04, what system are you using, @MauritsEvers? Is it a platform specific problem? - NickZengtmp
entries areNA
. - Maurits Evers