本文介绍了dplyr :: case_when评估错误:未找到对象“ x”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道为什么 dplyr :: case_when()在以下代码中产生错误吗?

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.

我在Ubuntu 16.04上使用R 3.4.3和dplyr 0.7.4。

I am using R 3.4.3 with dplyr 0.7.4 on Ubuntu 16.04.

该错误消息非常令人困惑,因为以下代码可以正常工作,这表明 x 不丢失:

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"
    )
  })


推荐答案

事实证明,这是一个错误,可能与混合评估程序有关:

As it turns out, this is a bug, probably related to the hybrid evaluator: https://github.com/tidyverse/dplyr/issues/3422

这篇关于dplyr :: case_when评估错误:未找到对象“ x”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-20 18:55