【问题标题】:Follow-up: Recognition of a function input by filter in R?后续:R中过滤器识别函数输入?
【发布时间】:2021-12-08 19:36:39
【问题描述】:

在我下面的foo() 调用中,有没有办法让dot_first... 中捕获的第一个元素)在函数内部被识别?

目前,dot_first 无法识别。

library(tidyverse)
library(rlang)

foo <- function(...){
  
  dots <- rlang::list2(...)  
  dot_first <- names(dots)[1]
  
  dat <- expand_grid(...)  
  dat %>%
    filter(!!dot_first != 1)
}

foo(study = 1:2, test = LETTERS[1:2])

【问题讨论】:

    标签: r function dplyr tidyverse rlang


    【解决方案1】:

    你可以使用 -

    library(tidyverse)
    
    foo <- function(...){
      dots <- list(...)  
      dot_first <- names(dots)[1]
      
      dat <- expand_grid(...)  
      dat %>%
        filter(.data[[dot_first]] != 1)
    }
    
    foo(study = 1:2, test = LETTERS[1:2])
    
    #  study test 
    #  <int> <chr>
    #1     2 A    
    #2     2 B    
    

    【讨论】:

      猜你喜欢
      • 2021-12-08
      • 1970-01-01
      • 2018-11-19
      • 1970-01-01
      • 2014-02-14
      • 2022-01-12
      • 1970-01-01
      • 1970-01-01
      • 2017-07-16
      相关资源
      最近更新 更多