'Create a function with a column name as an argument (with grouping)

I want to write a function that takes a data frame, grouping a variable(a column) and a variable (also a column). From reading multiple stakcflow attempts at cracking this, mostly recommended using arguments as strings.

My initial code to check for normality using the Shapiro-Wilk test for multiple data frames and variables was unsuccessful.

check_normality <- function(d, x_grouping_variable, y_cont_var){
   d %>%
      group_by([[x_grouping_variable]]) %>%
      summarise(`W Statistic` = shapiro.test([[y_cont_var]])$statistic,
                `p-value` = shapiro.test([[y_cont_var]])$p.value)

   return(shapiro.test([[y_cont_var]])$p.value)
}

ERROR:

Error: unexpected '[[' in "  return(shapiro.test([["

My attempt to fix it using this code was also unsuccessful.

check_normality <- function(d, x_grouping_variable, y_cont_var){
  d %>%
    group_by(((!! sym(x_grouping_variable)))) %>%
    summarise(`W Statistic` = shapiro.test((!! sym(y_cont_var)))$statistic,
          `p-value` = shapiro.test((!! sym(y_cont_var)))$p.value)

  return(shapiro.test((!! sym(y_cont_var)))$p.value)
}

check_normality(df, "RHF", "duratoin_days")

the error :

Error in !sym(y_cont_var) : invalid argument type
3.stopifnot(is.numeric(x))
2.shapiro.test((!!sym(y_cont_var)))
1.check_normality(df, "RHF", "duratoin_days")


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source