I recently started using R to work on my research data (and have definitely not regretted leaving SPSS) and can't find a way to solve the following problem: I have created a function which groups my data by a binary variable (did the patient suffer a certain type of complication yes/no? -> reg_var) and runs the summarise function of dplyr on a continuous variable which is associated to the binary variable (how high was the estimated risk for mentioned complication -> reg_yr). I now want to run this function for multiple pairs of variables (e.g. compare(reg_var1, reg_yr1), compare(reg_var2, reg_yr2) and compare(reg_var3, reg_yr3)) and create multiple tibbles which I can merge later. I have created two vectors containing the names of the variables (v_reg_var and v_reg_yr).
library(tidyverse)
# Create a function to calculate and compare est. risk percentages
# of patients with/without actual complications
compare <-function(reg_var, reg_yr) {
datatable %>%
group_by(.data[[reg_var]]) %>%
summarise(
n(), mean(.data[[reg_yr]]), sd(.data[[reg_yr]]), median(.data[[reg_yr]]), min(.data[[reg_yr]]), max(.data[[reg_yr]]),
"25%" = quantile(.data[[reg_yr]], probs = 0.25),
"50%" = quantile(.data[[reg_yr]], probs = 0.5),
"75%" = quantile(.data[[reg_yr]], probs = 0.75))
}
v_reg_var <- c[reg_var1, reg_var2, reg_var3, …)
v_reg_yr <- c[reg_yr1, reg_yr2, reg_yr3, …)
# Now if I run compare() using two vectors which only contain one character string it works just
# fine but unfortunately if I run compare(v_reg_var, v_reg_yr), I receive the following error:
compare(v_reg_var, v_reg_yr)
Error: Problem with \mutate()` input `..1`.`
x Must subset the data pronoun with a string
ℹ Input \..1` is `<unknown>`.`
It’d be great if anyone could give me a hint at what I’m doing wrong here or if there’s a more elegant solution.