fe108
fe108

Reputation: 501

Using grouping sets in a custom function - R

I am working in R and I have the following data:

data2<- data.frame(region_name = c("West", "West", "East"),  
                   type = c("small", "big", "big"), 
                   beta_rate = 7:9,
                   gamma_rate = 4:6)

I perform grouping sets on it to aggregate totals:

data_i_want <- data2 %>%
              as.data.table() %>% groupingsets(
                                  j = .(
                                         beta_total = sum(beta_rate),
                                         gamma_total = sum(gamma_rate)),
                                  by = c("region_name","type"),
                                  sets = list(
                                    c("region_name"),
                                    c("region_name","type"))
)

Output:

region_name type beta_total gamma_total
West NA 15 9
East NA 9 6
West small 7 4
West big 8 5
East big 9 6

My question: Can grouping sets be added into a custom function? E.g. this is what I have so far:

my_function <- function(data, var1, var2) { 
  
  data_final <- data %>%
                as.data.table() %>% groupingsets(
                  j = .(
                    beta_total = sum({{var1}}),
                    gamma_total = sum({{var2}})),
                                  by = c("region_name","type"),
                                  sets = list(
                                    c("region_name"),
                                    c("region_name","type"))
                )
}

However when I run the function:

data_i_want <- my_function(data2, beta_rate, gamma_rate)

I get this error:

Error in [.data.table(x, 0L, eval(jj), by) : object 'var1' not found

Is anyone able to help?

Upvotes: 1

Views: 67

Answers (1)

akrun
akrun

Reputation: 887148

groupingsets is from data.table. So, it may not work with the curly-curly operator ({{}}). An option will be to create an expression and then evalutate`

 my_function <- function(data, var1, var2) { 
     
     var1 <- deparse(substitute(var1))
     var2 <- deparse(substitute(var2))
     
    eval(parse(text = glue::glue("data %>%
                   as.data.table()  %>%
                   groupingsets(
                                            j = .(
                                              beta_total = sum({var1}),
                                              gamma_total = sum({var2})),
                                                            by = c('region_name','type'),
                                                            sets = list(
                                                              c('region_name'),
                                                              c('region_name','type'))
                                          )")))
                   
    
   }

-testing

my_function(data2, beta_rate, gamma_rate)
   region_name   type beta_total gamma_total
        <char> <char>      <int>       <int>
1:        West   <NA>         15           9
2:        East   <NA>          9           6
3:        West  small          7           4
4:        West    big          8           5
5:        East    big          9           6

Upvotes: 0

Related Questions