R:遍历自定义dplyr函数

时间:2019-03-18 11:03:42

标签: r loops dplyr purrr nse

我想构建一个自定义的dplyr函数,并理想地使用purrr :: map在其上进行迭代以保留在tidyverse中。

为了使事情变得尽可能简单,我使用一个非常简单的汇总函数来复制我的问题。

当使用dplyr构建自定义功能时,我遇到了非标准评估(NSE)问题。我发现了三种不同的处理方式。当直接调用该函数时,每种处理NSE的方法都可以正常工作,但在遍历该函数时却不能。在下面,您将找到复制我的问题的代码。使我的函数与purrr :: map一起工作的正确方法是什么?

    # loading libraries
    library(dplyr)
    library(tidyr)
    library(purrr)

    # generate test data
    test_tbl <- rbind(tibble(group = rep(sample(letters[1:4], 150, TRUE), each = 4),
                             score = sample(0:10, size = 600, replace = TRUE)),

                      tibble(group = rep(sample(letters[5:7], 50, TRUE), each = 3),
                             score = sample(0:10, size = 150, replace = TRUE))
    )




    # generate two variables to loop over
    test_tbl$group2 <- test_tbl$group
    vars <- c("group", "group2")


    # summarise function 1 using enquo()
    sum_tbl1 <- function(df, x) {

        x <- dplyr::enquo(x)

        df %>%
            dplyr::group_by(!! x) %>%
            dplyr::summarise(score = mean(score, na.rm =TRUE),
                             n = dplyr::n())

    }

    # summarise function 2 using .dots = lazyeval
    sum_tbl2 <- function(df, x) {

        df %>%
            dplyr::group_by_(.dots = lazyeval::lazy(x)) %>%
            dplyr::summarize(score = mean(score, na.rm =TRUE),
                             n = dplyr::n())

    }

    # summarise function 3 using ensym()
    sum_tbl3 <- function(df, x) {

        df %>%
            dplyr::group_by(!!rlang::ensym(x)) %>%
            dplyr::summarize(score = mean(score, na.rm =TRUE),
                             n = dplyr::n())

    }


    # Looping over the functions with map
    # each variation produces an error no matter which function I choose

    # call within anonymous function without pipe
    map(vars, function(x) sum_tbl1(test_tbl, x))
    map(vars, function(x) sum_tbl2(test_tbl, x))
    map(vars, function(x) sum_tbl3(test_tbl, x))

    # call within anonymous function witin pipe
    map(vars, function(x) test_tbl %>% sum_tbl1(x))
    map(vars, function(x) test_tbl %>% sum_tbl2(x))
    map(vars, function(x) test_tbl %>% sum_tbl3(x))

    # call with formular notation without pipe
    map(vars, ~sum_tbl1(test_tbl, .x))
    map(vars, ~sum_tbl2(test_tbl, .x))
    map(vars, ~sum_tbl3(test_tbl, .x))

    # call with formular notation within pipe
    map(vars,  ~test_tbl %>% sum_tbl1(.x))
    map(vars,  ~test_tbl %>% sum_tbl2(.x))
    map(vars,  ~test_tbl %>% sum_tbl3(.x))

我知道还有其他解决方案可用于循环生成汇总表,例如直接调用map并在map内部创建匿名函数(请参见下面的代码)。但是,我感兴趣的问题是通常如何在循环中处理NSE。

# One possibility to create summarize tables in loops with map
 vars %>%
    map(function(x){
        test_tbl %>%
            dplyr::group_by(!!rlang::ensym(x)) %>%
            dplyr::summarize(score = mean(score, na.rm =TRUE),
                             n = dplyr::n())
    })

更新:

在akrun下面,提供了一种使通过purrr :: map()进行调用的解决方案。然后,只能通过直接将分组变量作为字符串调用

,才能直接调用该函数
sum_tbl(test_tbl, “group”)

或间接为

sum_tbl(test_tbl, vars[1])

在此解决方案中,无法以常规dplyr方式将分组变量称为

sum_tbl(test_tbl, group)

最终,在我看来,自定义dpylr函数中NSE的解决方案可以在函数调用本身的级别上解决该问题,然后无法使用map / lapply,或者可以将NSE理解为可用于迭代,那么变量只能称为“字符串”。

基于akruns的答案,我构建了一个变通方法,该函数允许在函数调用中同时包含字符串和常规变量名。但是,肯定有更好的方法可以实现这一目标。理想情况下,在自定义dplyr函数中有一种处理NSE的更直接的方法,因此首先不需要像下面这样的变通方法。

sum_tbl <- function(df, x) {

        x_var <- dplyr::enquo(x)

        x_env <- rlang::get_env(x_var)

        if(identical(x_env,empty_env())) {

            # works, when x is a string and in loops via map/lapply
            sum_tbl <- df %>%
                dplyr::group_by(!! rlang::sym(x)) %>%
                dplyr::summarise(score = mean(score, na.rm = TRUE),
                                 n = dplyr::n())

        } else {
            # works, when x is a normal variable name without quotation marks
            x = dplyr::enquo(x)

            sum_tbl <- df %>%
                dplyr::group_by(!! x) %>%
                dplyr::summarise(score = mean(score, na.rm = TRUE),
                                 n = dplyr::n())
        }

        return(sum_tbl)
    }

最终更新/解决方案

在他的答案的更新版本中,akrun提供了一种解决方案,该解决方案说明了调用变量x的四种方式:

  1. 作为常规(非字符串)变量名称:sum_tbl(test_tbl, group)
  2. 作为字符串名称:sum_tbl(test_tbl, "group")
  3. 作为索引向量:sum_tbl(test_tbl, !!vars[1])
  4. 并作为purr::map()中的向量:map(vars, ~ sum_tbl(test_tbl, !!.x))

在(3)和(4)中,必须使用!!取消对变量x的引用。

如果我只为我自己使用该功能,那不是问题,但是一旦其他团队成员使用该功能,我就需要解释并记录该功能。

为避免这种情况,我现在扩展了akrun的解决方案,以在不取消报价的情况下考虑所有四种方式。但是,我不确定此解决方案是否会带来其他陷阱。

sum_tbl <- function(df, x) {

    # if x is a symbol such as group without strings, than turn it into a string    
    if(is.symbol(get_expr(enquo(x))))  {

        x <- quo_name(enquo(x))

    # if x is a language object such as vars[1], evaluate it
    # (this turns it into a symbol), then turn it into a string
    } else if (is.language(get_expr(enquo(x))))  {

        x <- eval(x)
        x <- quo_name(enquo(x))

    } 

      # this part of the function works with normal strings as x
        sum_tbl <- df %>%
            dplyr::group_by(!! rlang::sym(x)) %>%
            dplyr::summarise(score = mean(score, na.rm = TRUE),
                             n = dplyr::n())

    return(sum_tbl)

}

1 个答案:

答案 0 :(得分:1)

我们只能使用可以将字符串作为参数的group_by_at

sum_tbl1 <- function(df, x) {



            df %>%
                dplyr::group_by_at(x) %>%
                dplyr::summarise(score = mean(score, na.rm =TRUE),
                                 n = dplyr::n())

        }

然后调用为

out1 <- map(vars, ~ sum_tbl1(test_tbl, .x))

或者另一种选择是转换为sym bol,然后在!!内求值(group_by

sum_tbl2 <- function(df, x) {



            df %>%
                dplyr::group_by(!! rlang::sym(x)) %>%
                dplyr::summarise(score = mean(score, na.rm =TRUE),
                                 n = dplyr::n())

        }

out2 <- map(vars, ~ sum_tbl2(test_tbl, .x))

identical(out1 , out2)
#[1] TRUE

如果指定其中一个参数,则不必提供第二个参数,因此也可以在没有匿名调用的情况下运行

map(vars, sum_tbl2, df = test_tbl)

更新

如果我们想在更新的OP帖子中提到的条件下使用它

sum_tbl3 <- function(df, x) {

           x1 <- enquo(x)
           x2 <- quo_name(x1)

            df %>%
                dplyr::group_by_at(x2) %>%
                dplyr::summarise(score = mean(score, na.rm =TRUE),
                                 n = dplyr::n())

        }


sum_tbl3(test_tbl, group)
# A tibble: 7 x 3
#  group score     n
#  <chr> <dbl> <int>
#1 a      5.43   148
#2 b      5.01   144
#3 c      5.35   156
#4 d      5.19   152
#5 e      5.65    72
#6 f      5.31    36
#7 g      5.24    42

sum_tbl3(test_tbl, "group")
# A tibble: 7 x 3
#  group score     n
#  <chr> <dbl> <int>
#1 a      5.43   148
#2 b      5.01   144
#3 c      5.35   156
#4 d      5.19   152
#5 e      5.65    72
#6 f      5.31    36
#7 g      5.24    42

或通过“ vars”致电

sum_tbl3(test_tbl, !!vars[1])
# A tibble: 7 x 3
#  group score     n
#  <chr> <dbl> <int>
#1 a      5.43   148
#2 b      5.01   144
#3 c      5.35   156
#4 d      5.19   152
#5 e      5.65    72
#6 f      5.31    36
#7 g      5.24    42

map

map(vars, ~ sum_tbl3(test_tbl, !!.x))
#[[1]]
# A tibble: 7 x 3
#  group score     n
#  <chr> <dbl> <int>
#1 a      5.43   148
#2 b      5.01   144
#3 c      5.35   156
#4 d      5.19   152
#5 e      5.65    72
#6 f      5.31    36
#7 g      5.24    42

#[[2]]
# A tibble: 7 x 3
#  group2 score     n
#  <chr>  <dbl> <int>
#1 a       5.43   148
#2 b       5.01   144
#3 c       5.35   156
#4 d       5.19   152
#5 e       5.65    72
#6 f       5.31    36
#7 g       5.24    42