【问题标题】:How to filter a variable inside a function in R?如何过滤R中函数内的变量?
【发布时间】:2020-08-21 04:43:47
【问题描述】:

我在 R 中需要一些非常简单的帮助。我定义了一个函数来执行一些操作,但在使用输入参数调用函数时我无法选择变量。

例如:使用mpg数据集仅供参考,我需要过滤掉所有disp> 2.0的列


mpg 

#Defining a simple function called select_fun

select_fun <- function(x)
  {

  a <- mpg %>% filter(x >  2) 

  return(a)

}

select_fun("disp")

Output: 

<chr> model disp  year  cyl  trans     drv  cty  hwy   class
audi    a4  1.8 1999    4   auto(l5)    f   18  29  p   compact
audi    a4  1.8 1999    4   manual(m5)  f   21  29  p   compact
audi    a4  2.0 2008    4   manual(m6)  f   20  31  p   compact
audi    a4  2.0 2008    4   auto(av)    f   21  30  p   compact
audi    a4  2.8 1999    6   auto(l5)    f   16  26  p   compact
audi    a4  2.8 1999    6   manual(m5)  f   18  26  p   compact
audi    a4  3.1 2008    6   auto(av)    f   18  27  p   compact

输出不正确,因为过滤后的值仍然存在。 也许我错过了一些非常简单和愚蠢的东西! 任何帮助将非常感激 谢谢!!

【问题讨论】:

    标签: r function filtering


    【解决方案1】:

    有多种方法可以解决这个问题:

    library(dplyr)
    library(rlang)
    

    1) 使用filter_at

    select_fun1 <- function(df, x) {
       a <- df %>% filter_at(vars(x), any_vars(. > 2))
       return(a)
    }
    

    2) 使用基 R 子集

    select_fun2 <- function(df, x) {
       a <- df[df[[x]] > 2,]
       return(a)
    }
    

    3) 使用非标准评估

    select_fun3 <- function(df, x) {
       a <- df %>% filter(!!sym(x) > 2)
       return(a)
    }
    

    检查 3 的结果是否相同。

    identical(select_fun1(mpg, 'displ'), select_fun2(mpg, 'displ'))
    #[1] TRUE
    identical(select_fun1(mpg, 'displ'), select_fun3(mpg, 'displ'))
    #[1] TRUE
    

    【讨论】:

    • 可能不小心点击了它。你的回答很准确!!谢谢,现在我可以使用类似的函数过滤掉所有变量的所有 z-score 范围。
    【解决方案2】:

    还有一个,几乎与 Ronak Shah 的 select_fun3 相同,但更短一些(感谢 curly-curly 运算符),而且您不需要用它引用变量名:

    select_fun4 <- function(df, x) {
       df %>% filter({{x}} > 2)
       }
    
    select_fun4(mpg, displ)
    # A tibble: 191 x 11
       manufacturer model      displ  year   cyl trans      drv     cty   hwy fl    class  
       <chr>        <chr>      <dbl> <int> <int> <chr>      <chr> <int> <int> <chr> <chr>  
     1 audi         a4           2.8  1999     6 auto(l5)   f        16    26 p     compact
     2 audi         a4           2.8  1999     6 manual(m5) f        18    26 p     compact
     3 audi         a4           3.1  2008     6 auto(av)   f        18    27 p     compact
     4 audi         a4 quattro   2.8  1999     6 auto(l5)   4        15    25 p     compact
     5 audi         a4 quattro   2.8  1999     6 manual(m5) 4        17    25 p     compact
     6 audi         a4 quattro   3.1  2008     6 auto(s6)   4        17    25 p     compact
     7 audi         a4 quattro   3.1  2008     6 manual(m6) 4        15    25 p     compact
     8 audi         a6 quattro   2.8  1999     6 auto(l5)   4        15    24 p     midsize
     9 audi         a6 quattro   3.1  2008     6 auto(s6)   4        17    25 p     midsize
    10 audi         a6 quattro   4.2  2008     8 auto(s6)   4        16    23 p     midsize
    # ... with 181 more rows
    

    【讨论】:

    • 谢谢,非常感谢
    猜你喜欢
    • 2020-02-01
    • 2011-06-30
    • 1970-01-01
    • 2021-07-21
    • 2021-04-02
    • 1970-01-01
    • 2021-06-26
    • 2012-03-05
    • 1970-01-01
    相关资源
    最近更新 更多