I am doing t-tests on variables within a dataframe:
library(rstatix)
df <- data.frame(grouping = c(rep("left", 50), rep("right", 50)),
var1 = (rnorm(100, mean=21, sd=3)))
var1_result <- df %>%
t_test(var1 ~ grouping, paired = TRUE, detailed = TRUE) %>%
rstatix::add_significance()
var1_result
I have this working with repeated lines of code for each variable, but would like to improve by calling a user-defined function instead. I tried
my_t_test <- function(dataset, parameter, grouping_variable) {
parameter <- dataset %>% t_test({{parameter}} ~ {{grouping_variable}}, paired = TRUE, detailed = TRUE) %>% add_significance()
return(parameter)
}
my_t_test(df, var1, grouping)
However, I am encountering the error: "Error in pull(): ! Can't extract columns that don't exist. ✖ Column ... doesn't exist."
I found a few posts that address calling df variables within a function written in dplyr style (e.g., How can I write a function in R which accepts column names like dplyr? & writing a scoped filter function in dplyr)
I tried the approach of writing my function with "..." instead as suggested by first post, but this did not work, and was having trouble generalizing any solutions from other posts. Very interested in learning more about proper notation and scoping with user-defined functions when using dplyr
You need to be a bit more careful when trying to put
{{}}expressions into a formula since the left and right side of the formula are left unevaluated. One possible work around would beHere we call the
~function to build the formula and useenexprto capture the appropriate symbols.This should produce the same output
Note that
{{}}is not a standard R syntax and only works for packages that userlangas a back end (mainly those in the "tidyverse"). It just so happens thatrstatix::t_testhappens to usedplyrin the back end