marc1s
marc1s

Reputation: 779

Passing parameters into function that uses dplyr

I have the following function to describe a variable

library(dplyr)
describe = function(.data, variable){
  args <- as.list(match.call())
  evalue = eval(args$variable, .data)
  summarise(.data,
            'n'= length(evalue),
            'mean' = mean(evalue),
            'sd' = sd(evalue))
}

I want to use dplyr for describing the variable.

set.seed(1)
df = data.frame(
  'g' = sample(1:3, 100, replace=T),
  'x1' = rnorm(100),
  'x2' = rnorm(100)
)
df %>% describe(x1)
#     n        mean        sd
# 1 100 -0.01757949 0.9400179

The problem is that when I try to apply the same descrptive using function group_by the describe function is not applied in each group

df %>% group_by(g) %>% describe(x1)
# # A tibble: 3 x 4
#       g     n        mean        sd
#   <int> <int>       <dbl>     <dbl>
# 1     1   100 -0.01757949 0.9400179
# 2     2   100 -0.01757949 0.9400179
# 3     3   100 -0.01757949 0.9400179

How would you change the function to obtain what is desired using an small number of modifications?

Upvotes: 7

Views: 166

Answers (2)

Sebastian Sauer
Sebastian Sauer

Reputation: 1673

Base NSE appears to work, too:

describe <- function(data, var){

  var_q <- substitute(var)
  data %>% 
    summarise(n = n(),
              mean = mean(eval(var_q)),
              sd = sd(eval(var_q)))
}


df %>% describe(x1) 

   n       mean       sd
1 100 -0.1266289 1.006795



df %>% group_by(g) %>% describe(x1)
# A tibble: 3 x 4
      g     n       mean       sd
  <int> <int>      <dbl>    <dbl>
1     1    33 -0.1379206 1.107412
2     2    29 -0.4869704 0.748735
3     3    38  0.1581745 1.020831

Upvotes: 0

r.bot
r.bot

Reputation: 5424

You need tidyeval:

describe = function(.data, variable){
  evalue = enquo(variable)
  summarise(.data,
            'n'= length(!!evalue),
            'mean' = mean(!!evalue),
            'sd' = sd(!!evalue))
}

df %>% group_by(g) %>% describe(x1)
# A tibble: 3 x 4
      g     n        mean        sd
  <int> <int>       <dbl>     <dbl>
1     1    27 -0.23852862 1.0597510
2     2    38  0.11327236 0.8470885
3     3    35  0.01079926 0.9351509

The dplyr vignette 'Programming with dplyr' has a thorough description of using enquo and !!

Edit:

In response to Axeman's comment, I'm not 100% why the group_by and describe does not work here. However, using debugonce with the funciton in it's original form

debugonce(describe)

df %>% group_by(g) %>% describe(x1)

one can see that evalue is not grouped and is just a numeric vector of length 100.

Upvotes: 7

Related Questions