dplyr::case_when() 莫名其妙地返回名称(消息)<- `*vtmp*` 错误

Ric*_*lly 19 r dplyr

在 R 版本 4.1.2 下的 dplyr_1.0.8中,一个非常简单的评估会dplyr::case_when()返回一条奇怪的错误消息。我已经隔离了此代码中的行为,durationI如果出现两种边缘情况之一,我将尝试调整变量的值:

library(tidyverse)

# Create simple example data
raw <- tribble(
  ~activity_ID, ~durationI, ~distanceI, ~tmode,
             1,        190,         57, "auto",
             2,         23,         41,     NA,
             3,         91,         58, "rail"
)

# Now trip it up
update <- mutate(raw,
  distanceI = ifelse(is.na(tmode), NA, distanceI),
  durationI = case_when(is.na(tmode) ~ NA, durationI > 180 ~ 180,
    TRUE ~ durationI))

# Should result in:
#   activity_ID, durationI, distanceI, tmode
#             1,       180,        57,  auto
#             2,        NA,        41,    NA
#             3,        91,        58,  rail
Run Code Online (Sandbox Code Playgroud)

当我运行此代码时,它会产生以下错误消息:

Error in `mutate()`:
! Problem while computing `durationI = case_when(is.na(tmode) ~
  NA, durationI > 180 ~ 180, TRUE ~ durationI)`.
Caused by error in `` names(message) <- `*vtmp*` ``:
! 'names' attribute [1] must be the same length as the vector [0]
Run `rlang::last_error()` to see where the error occurred.
Run Code Online (Sandbox Code Playgroud)

当我运行时,rlang::last_error()它同样没有帮助:

<error/dplyr:::mutate_error>
Error in `mutate()`:
! Problem while computing `durationI = case_when(is.na(mode) ~
  NA, durationI > 180 ~ 180, TRUE ~ durationI)`.
Caused by error in `` names(message) <- `*vtmp*` ``:
! 'names' attribute [1] must be the same length as the vector [0]
Backtrace:
  1. dplyr::mutate(...)
  6. dplyr::case_when(...)
  7. dplyr:::replace_with(...)
  8. dplyr:::check_type(val, x, name, error_call = error_call)
  9. rlang::abort(msg, call = error_call)
 10. rlang:::signal_abort(cnd, .file)
 11. base::signalCondition(cnd)
 13. rlang:::conditionMessage.rlang_error(cond)
 14. rlang::cnd_message(c)
 15. rlang:::cnd_message_format(cnd, ...)
 16. cli::cli_format(glue_escape(lines), .envir = emptyenv())
Run `rlang::last_trace()` to see the full context.
Run Code Online (Sandbox Code Playgroud)

如果我检查所有变量的长度,它们当然都是相同的长度。我很困惑。我缺少什么?

jpi*_*sen 36

你会遇到这个问题,因为你试图混合逻辑向量和数字向量。

\n

在你的case_when声明中:

\n
case_when(\n  is.na(tmode) ~ NA,\n  durationI > 180 ~ 180,\n  TRUE ~ durationI\n)\n
Run Code Online (Sandbox Code Playgroud)\n

您的第一个案例的评估结果为NA。这使得 R 认为您需要一个逻辑向量。当下一行计算为数字时,您会收到错误。

\n

您可以通过替换NA为 numeric 类型的缺失值来修复此问题NA_real_

\n
raw %>% \n  mutate(\n    distanceI = ifelse(is.na(tmode), NA, distanceI),\n    durationI = case_when(\n      is.na(tmode) ~ NA_real_,\n      durationI > 180 ~ 180,\n      TRUE ~ durationI\n    )\n  )\n#> # A tibble: 3 \xc3\x97 4\n#>   activity_ID durationI distanceI tmode\n#>         <dbl>     <dbl>     <dbl> <chr>\n#> 1           1       180        57 auto \n#> 2           2        NA        NA <NA> \n#> 3           3        91        58 rail\n
Run Code Online (Sandbox Code Playgroud)\n