根据外部值有条件地应用管道步骤 [英] Conditionally apply pipeline step depending on external value

查看:79
本文介绍了根据外部值有条件地应用管道步骤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出dplyr工作流程:

Given the dplyr workflow:

require(dplyr)                                      
mtcars %>% 
    tibble::rownames_to_column(var = "model") %>% 
    filter(grepl(x = model, pattern = "Merc")) %>% 
    group_by(am) %>% 
    summarise(meanMPG = mean(mpg))

我对根据applyFilter的值有条件地应用filter感兴趣.

I'm interested in conditionally applying filter depending on the value of applyFilter.

对于applyFilter <- 1,使用"Merc"字符串过滤行,而不返回过滤器所有行.

For applyFilter <- 1 the rows are filtered with use of the "Merc" string, without the filter all rows are returned.

applyFilter <- 1


mtcars %>%
  tibble::rownames_to_column(var = "model") %>%
  filter(model %in%
           if (applyFilter) {
             rownames(mtcars)[grepl(x = rownames(mtcars), pattern = "Merc")]
           } else
           {
             rownames(mtcars)
           }) %>%
  group_by(am) %>%
  summarise(meanMPG = mean(mpg))

问题

由于始终评估ifelse调用,因此建议的解决方案效率低下;一种更理想的方法只会评估applyFilter <- 1filter步骤.

Problem

The suggested solution is inefficient as the ifelse call is always evaluated; a more desireable approach would only evaluate the filter step for applyFilter <- 1.

效率低下的工作解决方案如下:

The inefficient working solution would look like that:

mtcars %>% 
    tibble::rownames_to_column(var = "model") %>% 
    # Only apply filter step if condition is met
    if (applyFilter) { 
        filter(grepl(x = model, pattern = "Merc"))
        }
    %>% 
    # Continue 
    group_by(am) %>% 
    summarise(meanMPG = mean(mpg))

自然,上面的语法不正确.仅仅是说明理想工作流程的外观.

Naturally, the syntax above is incorrect. It's only a illustration how the ideal workflow should look.

  • 我对创建临时对象不感兴趣;工作流程应类似于:

  • I'm not interested in creating an interim object; the workflow should resemble:

startingObject
    %>%
    ...
    conditional filter
    ...
    final object

  • 理想情况下,我想提供一个解决方案,在该解决方案中,我可以控制是否评估filter调用

    推荐答案

    这种方法怎么样:

    mtcars %>% 
        tibble::rownames_to_column(var = "model") %>% 
        filter(if(applyfilter== 1) grepl(x = model, pattern = "Merc") else TRUE) %>% 
        group_by(am) %>% 
        summarise(meanMPG = mean(mpg))
    

    这意味着grepl仅在applyfilter为1时才被求值,否则filter只是回收TRUE.

    This means grepl is only evaluated if the applyfilter is 1, otherwise the filter simply recycles a TRUE.

    或者另一个选择是使用{}:

    Or another option is to use {}:

    mtcars %>% 
      tibble::rownames_to_column(var = "model") %>% 
      {if(applyfilter == 1) filter(., grepl(x = model, pattern = "Merc")) else .} %>% 
      group_by(am) %>% 
      summarise(meanMPG = mean(mpg))
    


    显然还有另一种可能的方法,您可以简单地断开管道,有条件地进行过滤,然后继续管道(我知道OP并没有要求这样做,只是想给其他读者一个例子)


    There's obviously another possible approach in which you would simply break the pipe, conditionally do the filter and then continue the pipe (I know OP didn't ask for this, just want to give another example for other readers)

    mtcars %<>% 
      tibble::rownames_to_column(var = "model")
    
    if(applyfilter == 1) mtcars %<>% filter(grepl(x = model, pattern = "Merc"))
    
    mtcars %>% 
      group_by(am) %>% 
      summarise(meanMPG = mean(mpg))
    

    这篇关于根据外部值有条件地应用管道步骤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆