1

I need to create a function that could group_by and summarise a data frame using the names of its columns. I'm working with dplyr version 0.4.1 (and I cannot update), so it looks like the solutions I've found on the other topics doesn't work...

Here is my example :

data <- data.frame(section=rep(c("A","B"),3), quantity=c(6:11))
#I need to get this result : 
RESULT = data %>% group_by(section) %>% summarise(total=sum(quantity))

I implemented this function, but I got an error :

# function : 
synthetize = function(x,column,measure){
  result = x %>% group_by(column) %>% summarise(total=sum(measure))
}
RESULT2=synthetize(data,column="section",measure="quantity")
RESULT2

I tried eval, get, but it looks like this doesn't help

Paugre
  • 11
  • 1

2 Answers2

2

We can convert the string to symbol with rlang::sym and evaluate (!!)

library(tidyverse)
synthetize = function(x, column, measure){
     x %>% 
        group_by_at(column) %>%
        summarise(total=sum(!! rlang::sym(measure)))
   }

synthetize(data, column="section", measure="quantity")
# A tibble: 2 x 2
#  section total
#   <fct>   <int>
#1 A          24
#2 B          27

NOTE: Here we use the OP's same argument type


If we are using older version of dplyr, may be the following would help

library(lazyeval)
synthetize2 = function(x, column, measure){

  x %>% 
     group_by_(column) %>%
     summarise(total = interp(~ sum(v1), v1 = as.name(measure)))


synthetize2(data, column='section', measure='quantity')
akrun
  • 874,273
  • 37
  • 540
  • 662
  • Thanks for your reply. I cannot use tidyverse either... So I get an error message: `Error in function_list[[i]](value) : impossible de trouver la fonction "group_by_at"` – Paugre Dec 17 '18 at 16:09
  • Indeed, I'm using an older version of `dplyr`. Still, it looks like `interp()` doesn't solve the issue : `Error in eval(substitute(expr), envir, enclos) : Not a vector` – Paugre Dec 17 '18 at 16:14
0

Another way is with enquo:

library(tidyverse)

synthetize = function(x,column,measure) {

  result = x %>% group_by(!! enquo(column)) %>% summarise(total := sum(!! enquo(measure)))

}

In this case, you wouldn't need to quote the variables:

RESULT2 = synthetize(data, column = section, measure = quantity)

RESULT2

# A tibble: 2 x 2
  section total
  <fct>   <int>
1 A          24
2 B          27

If you don't have access to newest tidyverse, try with get:

library(dplyr)

synthetize = function(x,column,measure) {

  result = x %>% group_by(get(column)) %>% summarise(total := sum(get(measure)))

}
arg0naut91
  • 14,574
  • 2
  • 17
  • 38
  • Thank you. Unfortunately, it doesn't work (due to 0.4.1 version of dplyr, I guess...) : `Error in mutate_impl(.data, dots) : objet 'section' introuvable` – Paugre Dec 17 '18 at 16:07
  • Try with the method I've just added. In this case you need to quote the arguments. – arg0naut91 Dec 17 '18 at 16:12
  • Again, thanks! the `get` solution doesn't solve the problem... : `Error in get("section") : objet 'section' introuvable` – Paugre Dec 17 '18 at 16:16
  • Hmm, that's probably then due to the previous version. Unfortunately I cannot downgrade and test easily. – arg0naut91 Dec 17 '18 at 16:18