Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Supplying multiple groups of variables to a function for dplyr arguments in the body

Here is the data:

library(tidyverse)

data <- tibble::tribble(
  ~var1, ~var2, ~var3,  ~var4,    ~var5,
    "a",   "d",   "g",  "hello",    1L,
    "a",   "d",   "h",  "hello",    2L,
    "b",   "e",   "h",  "k",        4L,
    "b",   "e",   "h",  "k",        7L,
    "c",   "f",   "i",  "hello",    3L,
    "c",   "f",   "i",  "hello",    4L
  )

and the vectors, I want to use:

filter_var <- c("hello")
groupby_vars1 <- c("var1", "var2", "var3")
groupby_vars2 <- c("var1", "var2")
joinby_vars1 <- c("var1", "var2")
joinby_vars2 <- c("var1", "var2", "var3")

2nd & 5th, and 3rd & 4th vectors are same, but please assume they are different and retain them as different vectors.

Now I want to create a generic function where I can take data and these vectors to get the results.

my_fun <- function(data, filter_var, groupby_vars1,groupby_vars2, joinby_vars1, joinby_vars2) {

  data2 <- data %>% filter(var4 == filter_var) 

  data3 <- data2 %>%
    group_by(groupby_vars1) %>% 
    summarise(var6 = sum(var5))

  data4 <- data3 %>%
    ungroup() %>%
    group_by(groupby_vars2) %>% 
    summarise(avg = mean(var6,na.rm = T))

  data5 <- data3 %>% left_join(data4, by = joinby_vars1)

  data6 <- data %>% left_join(data5, by = joinby_vars2)
}

The problem is of supplying multiple vectors of multiple variables to a function to be used as dplyr arguments in the body. I tried looking into the http://dplyr.tidyverse.org/articles/programming.html, but could not solve the above problem.

like image 803
Geet Avatar asked Apr 24 '18 23:04

Geet


People also ask

What special operator is used by dplyr to pass a function argument to one of its methods?

dplyr utilizes pipe operator from another package (magrittr). It allows you to write sub-queries like we do it in sql. Note : All the functions in dplyr package can be used without the pipe operator.

Can you use dplyr in a function?

As with any R function, you can think of functions in the dplyr package as verbs - that refer to performing a particular action on a data frame. The core dplyr functions are: rename() renames columns. filter() filters rows based on their values in specified columns.

What does the Group_by function do in R?

The group_by() function in R is from dplyr package that is used to group rows by column values in the DataFrame, It is similar to GROUP BY clause in SQL. R dplyr groupby is used to collect identical data into groups on DataFrame and perform aggregate functions on the grouped data.


1 Answers

group_by cannot take groupby_vars... strings as input. You need to use rlang::syms() to turn string vector into variables then use !!! to unquote them so that they can be evaluated inside group_by

library(tidyverse)
library(rlang)

data <- tibble::tribble(
  ~var1, ~var2, ~var3,  ~var4,    ~var5,
  "a",   "d",   "g",  "hello",    1L,
  "a",   "d",   "h",  "hello",    2L,
  "b",   "e",   "h",  "k",        4L,
  "b",   "e",   "h",  "k",        7L,
  "c",   "f",   "i",  "hello",    3L,
  "c",   "f",   "i",  "hello",    4L
)

filter_var <- c("hello")
groupby_vars1 <- c("var1", "var2", "var3")
groupby_vars2 <- c("var1", "var2")
joinby_vars1  <- c("var1", "var2")
joinby_vars2  <- c("var1", "var2", "var3")

my_fun <- function(data, filter_var, 
                   groupby_vars1, groupby_vars2, 
                   joinby_vars1,  joinby_vars2) {

  groupby_vars1 <- syms(groupby_vars1)
  groupby_vars2 <- syms(groupby_vars2)

  data2 <- data %>% 
    filter(var4 == filter_var) 

  data3 <- data2 %>%
    group_by(!!! groupby_vars1) %>% 
    summarise(var6 = sum(var5))

  data4 <- data3 %>%
    ungroup() %>%
    group_by(!!! groupby_vars2) %>% 
    summarise(avg = mean(var6, na.rm = TRUE))

  data5 <- data3 %>% 
    left_join(data4, by = joinby_vars1)

  data6 <- data %>% 
    left_join(data5, by = joinby_vars2)

  return(data6)
}

my_fun(data, filter_var, 
       groupby_vars1, groupby_vars2, 
       joinby_vars1,  joinby_vars2)

#> # A tibble: 6 x 7
#>   var1  var2  var3  var4   var5  var6   avg
#>   <chr> <chr> <chr> <chr> <int> <int> <dbl>
#> 1 a     d     g     hello     1     1   1.5
#> 2 a     d     h     hello     2     2   1.5
#> 3 b     e     h     k         4    NA  NA  
#> 4 b     e     h     k         7    NA  NA  
#> 5 c     f     i     hello     3     7   7  
#> 6 c     f     i     hello     4     7   7

Another way to do it: parse the string vector using parse_exprs outside then unquote them inside the function. See also this

my_fun2 <- function(data, filter_var, 
                   groupby_vars1, groupby_vars2, 
                   joinby_vars1,  joinby_vars2) {

  data2 <- data %>% 
    filter(var4 == filter_var) 

  data3 <- data2 %>%
    group_by(!!! groupby_vars1) %>% 
    summarise(var6 = sum(var5))

  data4 <- data3 %>%
    ungroup() %>%
    group_by(!!! groupby_vars2) %>% 
    summarise(avg = mean(var6, na.rm = TRUE))

  data5 <- data3 %>% 
    left_join(data4, by = joinby_vars1)

  data6 <- data %>% 
    left_join(data5, by = joinby_vars2)

  return(data6)
}

my_fun2(data, filter_var, 
        parse_exprs(groupby_vars1), parse_exprs(groupby_vars2), 
        joinby_vars1,  joinby_vars2) 

identical(my_fun(data, filter_var, 
                 groupby_vars1, groupby_vars2, 
                 joinby_vars1,  joinby_vars2),
          my_fun2(data, filter_var, 
                  parse_exprs(groupby_vars1), parse_exprs(groupby_vars2), 
                  joinby_vars1,  joinby_vars2))

[1] TRUE                      

Created on 2018-04-24 by the reprex package (v0.2.0).

like image 99
Tung Avatar answered Oct 02 '22 07:10

Tung