简体   繁体   English

在 mutate/case_when 中使用自定义矢量化 function 时出错

[英]Error in using custom vectorized function in mutate/case_when

Below is a simple code to reproduce the error.下面是重现错误的简单代码。 I define a simple function, vectorize it with another function using purrr::map , and then try to use it in a mutate case_when where the condition should normally ensure that the arguments are valid.我定义了一个简单的 function,使用purrr::map将其与另一个 function 向量化,然后尝试在 mutate case_when 中使用它,其中条件通常应确保 arguments 有效。 The error occurs in the condition if(arg1 > 0) when arg1 = NA , but I don't understand why that even happens.错误发生在条件if(arg1 > 0) when arg1 = NA时,但我不明白为什么会发生这种情况。 If I apply the filter, the error disappears.如果我应用过滤器,错误就会消失。 Does anyone have an idea what I'm doing wrong?有谁知道我做错了什么? My feeling is that it should work.我的感觉是它应该工作。

require(tidyverse)

f_single <- function(arg1, arg2) {
  if (arg1 > 0) {
    return(arg1 * arg2)
  }
}

f_vector <- function(arg1, arg2) {
  result <- map2_dbl(arg1, arg2, f_single)
  return(result)
}

x <- tribble(~ arg1, ~ arg2,
             NA, 1,
             2, 3,
             4, 5,)

x %>%
  # filter(!is.na(arg1)) %>%
  mutate(y = case_when(arg1 > 0 ~ f_vector(arg1, arg2)))

The error is the following:错误如下:

Error in `mutate()`:
! Problem while computing `y = case_when(arg1 > 0 ~ f_vector(arg1, arg2))`.
Caused by error in `if (arg1 > 0) ...`:
! missing value where TRUE/FALSE needed

Two issues:两个问题:

  1. Passing NA to an if statement will throw an error.NA传递给if语句将引发错误。 You can avoid this by wrapping the condition with isTRUE .您可以通过使用isTRUE包装条件来避免这种情况。
  2. Your code will still throw an error, because f_single returns NULL when arg1 is missing or <= 0, but map_* expects a return value for every input.您的代码仍会引发错误,因为当arg1缺失或 <= 0 时f_single返回NULL ,但map_*期望每个输入都有一个返回值。

Changing f_single as follows will solve both problems:如下更改f_single将解决这两个问题:

f_single <- function(arg1, arg2) {
  if (isTRUE(arg1 > 0)) {
    arg1 * arg2
  } else {
    NA_real_
  }
}

# rest of code unchanged from original

# # A tibble: 3 x 3
#    arg1  arg2     y
#   <dbl> <dbl> <dbl>
# 1    NA     1    NA
# 2     2     3     6
# 3     4     5    20

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM