整洁评估:如何在依赖 %>% 管道的自定义函数中使用 dplyr::na_if 作为可选参数

Tidy Evaluation: How to use dplyr::na_if as an optional argument in a custom function that relies on a %>% pipe

我正在尝试编写一个函数,它接受数据框,将列从 chr 转换为 dbl,然后将 1 添加到列。我还想 可选地 将某些值替换为 NA。否则,如果不使用相关参数,我希望函数跳过 NA 替换步骤。

数据

library(tibble)
library(dplyr)
library(magrittr)

df <-
  tibble(id = 1:10, col_of_interest = 21:30) %>%
  add_row(id = 11, col_of_interest = 999) %>%
  mutate(across(col_of_interest, as.character))

df

## # A tibble: 11 x 2
##       id col_of_interest
##    <dbl> <chr>          
##  1     1 21             
##  2     2 22             
##  3     3 23             
##  4     4 24             
##  5     5 25             
##  6     6 26             
##  7     7 27             
##  8     8 28             
##  9     9 29             
## 10    10 30             
## 11    11 999  

编写函数

函数应该:

  1. 接收数据。
  2. col_of_interestchr 转换为 dbl
  3. 999 替换为 NA 但前提是我指定 999 应替换为 NA
  4. 1添加到col_of_interest

我的尝试

在编写我的函数时,我得到了两个资源的指导:

  1. 使用 {{ var }} 将数据变量传递到函数参数中,如 here 所述。
  2. if的使用基于
add_one <- function(data, var, na_if_val = NULL) {

  data %>%

    mutate(across({{ var  }}, as.numeric)) %>%
    
    {if( is.null( {{ na_if_val }} )
    ) .  # <--- the dot means: "return the preexisting dataframe"

      else

        na_if( {{ na_if_val }} )

    } %>%
    
    mutate(across({{ var  }}, add, 1))
}

当我在我的 df 对象上测试函数时出现错误。

add_one(data = df,
        var = col_of_interest,
        na_if_val = "999")

Error in check_length(y, x, fmt_args("y"), glue("same as {fmt_args(~x)}")) : argument "y" is missing, with no default

谷歌搜索此错误产生 this page,指出:

Note, however, that na_if() can only take arguments of length one.

但是,在 add_one 函数的管道中仅包含 na_if( {{ na_if_val }} ) 确实有效。条件评估与 is.null 相结合导致函数中断。我不明白为什么。

你有几个问题,但最主要的是因为你 non-stardard 评估错误。

add_one <- function(data, var, na_if_val = NULL) {
  
  var_b <- enquo(var)
  
  data <- data %>%
    mutate(across(!!var_b, as.numeric)) 
  
   if(!is.null(na_if_val)){
     data <- data %>% 
       mutate(across(!!var_b, na_if, y = na_if_val))
   }
   
  data <- data %>% 
    mutate(across(!!var_b, add, 1))
  
  return(data)
}

返回这个:

add_one(df, col_of_interest, 999)

# A tibble: 11 x 2
      id col_of_interest
   <dbl>           <dbl>
 1     1              22
 2     2              23
 3     3              24
 4     4              25
 5     5              26
 6     6              27
 7     7              28
 8     8              29
 9     9              30
10    10              31
11    11              NA

首先,您需要使用 enquo() 函数引用感兴趣的变量,然后,在您需要的地方取消引用该变量(使用 bang bang !!)。你的函数的另一个问题是在管道中间插入你的 if 语句,这是行不通的。如果需要在特殊情况下应用某些方法,则需要与主要计算分开评估。

我通过简单地指定 drop_naxy 参数解决了这个问题。

add_one <- function(data, var, na_if_val = NULL) {

  data %>%

    mutate(across({{ var  }}, as.numeric)) %>%
    
    {if( is.null( {{ na_if_val }} )
    ) .  # <--- the dot means: "return the preexisting dataframe"

      else

        na_if(x = ., y = {{ na_if_val }} ) ## <-- change is here

    } %>%
    
    mutate(across({{ var  }}, add, 1))
}


add_one(data = df,
        var = col_of_interest,
        na_if_val = 999)

## # A tibble: 11 x 2
##       id col_of_interest
##    <dbl>           <dbl>
##  1     1              22
##  2     2              23
##  3     3              24
##  4     4              25
##  5     5              26
##  6     6              27
##  7     7              28
##  8     8              29
##  9     9              30
## 10    10              31
## 11    11              NA

编辑

我在@LionelHenry 的评论后 na_if_val 左右删除了 {{ }}

add_one <- function(data, var, na_if_val = NULL) {

  data %>%

    mutate(across({{ var  }}, as.numeric)) %>%

    {if( is.null(na_if_val)
    ) .  # <--- the dot means: "return the preexisting dataframe"

      else

        na_if(x = ., y = na_if_val)

    } %>%

    mutate(across({{ var  }}, add, 1))
}