group_by and summarize used in a function

Hello,
I am trying to make a function with summarize, to use with several dataframes. I guess I am missing something trivial, but cannot figure out what.
A basic call I am trying to build on is:

library(tidyverse)

mtcars |> group_by(cyl) |> summarize("mean"= mean(mpg), "sd"= sd(mpg), "median" = median(mpg))

My attempt at a function is:

library(tidyverse)

f.stat <- function(df,dv){
  df |> group_by(cyl) |> summarize("mean"= mean(dv), "sd"= sd(dv), "median" = median(dv))
}

f.stat(mtcars, mpg)

It does not work with the dv supplied this way, but I don't know how to fix it. How should I do it properly?

Programming with the tidyverse can be annoying sometimes! Read up on {rlang}:

library(tidyverse, warn.conflicts = FALSE)

f.stat <- function(df,dv){
  df |> 
    group_by(cyl) |> 
    summarize("mean" = mean({{dv}}),
              "sd" = sd({{dv}}),
              "median" = median({{dv}}))
}

f.stat(mtcars, mpg)
#> # A tibble: 3 × 4
#>     cyl  mean    sd median
#>   <dbl> <dbl> <dbl>  <dbl>
#> 1     4  26.7  4.51   26  
#> 2     6  19.7  1.45   19.7
#> 3     8  15.1  2.56   15.2

Created on 2022-07-02 by the reprex package (v2.0.1)

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.