Programming with dplyr using a string as input

I would like to write a function that uses dplyr inside, and I provide variable names as strings. Unfortunately, using NSE for dplyr makes it quite complicated. From programming with dplyr, I get the following example

my_summarise <- function(df, var) {
  var <- enquo(var)

  df %>%
    group_by(!!var) %>%
    summarise(a = mean(a))
}

my_summarise(df, g1)

However, I would like to write a function where instead g1I could provide "g1"and I cannot turn around how to do this.

+13
source share
2 answers

As far as I know, you can use as.nameor sym(from the package rlang- I don't know if it will be dplyrimported in the end):

library(dplyr)
my_summarise <- function(df, var) {
  var <- rlang::sym(var)
  df %>%
    group_by(!!var) %>%
    summarise(mpg = mean(mpg))
}

or

my_summarise <- function(df, var) {
  var <- as.name(var)
  df %>%
    group_by(!!var) %>%
    summarise(mpg = mean(mpg))
}

my_summarise(mtcars, "cyl")
# # A tibble: 3 ร— 2
#     cyl      mpg
#   <dbl>    <dbl>
# 1     4 26.66364
# 2     6 19.74286
# 3     8 15.10000
+16

.data rlang - , , .

.data

my_summarise <- function(df, var) {
     df %>%
          group_by(.data[[var]]) %>%
          summarise(mpg = mean(mpg))
}

my_summarise(mtcars, "cyl")
# A tibble: 3 x 2
    cyl   mpg
  <dbl> <dbl>
1     4  26.7
2     6  19.7
3     8  15.1
0

Source: https://habr.com/ru/post/1681989/


All Articles