Create a function with dplyr with two env-variables

Create a function with dplyr with two env-variables

I am trying to write functions when there are two env-variables. This vignette has multiple examples with one env variable and multiple data variables, but no examples with two env variables.

I could not find a solution at https://adv-r.hadley.nz/ either.

As an example, I start with two data frames. First, I want to join them. Then I want to compute some summary statistics. I want to create a function that can do the work.

Note that the number of grouping variables (such as state and people) may change depending on the example. Additionally, the variables that are being summed (such as sales and profit) may also change.


# I need a function

Compute = function(df1, df2, grp_vars, compute_vars) {code}


# An interactive solution: 
library(dplyr)


sales_data = data.frame(staffID = rep(1:5, each = 5),
                 state = c(rep('Cal', 13), rep('Wash', 12)),
                 sales = 101:125,
                 profit = 11:35
                 )

sales_data

staff = data.frame(staffID = 1:5,
                   people = c('Al', 'Barb', 'Carol', 'Dave', 'Ellen'))

staff

res1 = sales_data %>% inner_join(staff, by = 'staffID')
res1

res2 = res1 %>% 
  group_by(state, people) %>% summarize(total_sales = sum(sales), total_profit = sum(profit))
res2
# If I only needed to summarize the data, this would work:

# From Programming with dplyr
my_summarise <- function(data, group_var, summarise_var) {
  data %>%
    group_by(across({{ group_var }})) %>% 
    summarise(across({{ summarise_var }}, sum, .names = "sum_{.col}"))
}

my_summarise(res1, c(state, people), c(sales, profit))

Summary. I need a function,
Compute = function(df1, df2, grp_vars, compute_vars) {code}
First join two data frames when both the joining/grouping variables and the computed variables are selected by the user.
Secondly, compute totals and return the results

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.