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.