I am making a function to conduct a t-test (for multiple temperature variables called temp_
).
I want to make a function that passes var_name
. For example, in the case of passing dataframe
, I made a function below and it was successfully executed:
balance_table <- function(df) {
table_result <- df %>%
rstatix::t_test(temp_ca ~ station) %>%
rstatix::adjust_pvalue(method = "BH") %>%
rstatix::add_significance()
table_result
}
balance_table(df_weather)
However, when passing a variable instead of df
below, I got the following error.
balance_table <- function(var_name) {
table_result <- df_weather %>%
# var_name <- enquo(var_name)
rstatix::t_test(var_name ~ station) %>%
# rstatix::t_test(!!var_name ~ station) %>%
rstatix::adjust_pvalue(method = "BH") %>%
rstatix::add_significance()
table_result
}
balance_table(temp_ca)
Error in `vec_as_location2_result()`:
! Can't extract columns that don't exist.
✖ Column `as.name(comp_var)` doesn't exist.
Run `rlang::last_error()` to see where the error occurred.
Called from: signal_abort(cnd)
Finally, the simple case without making my function works well.
df_weather %>%
rstatix::t_test(temp_ca ~ station) %>%
rstatix::adjust_pvalue(method = "BH") %>%
rstatix::add_significance()
# A tibble: 1 × 10
.y. group1 group2 n1 n2 statistic df p p.adj
<chr> <chr> <chr> <int> <int> <dbl> <dbl> <dbl> <dbl>
1 temp_ca T C 124 124 0.648 237. 0.518 0.518
# … with 1 more variable: p.adj.signif <chr>
I tried many patterns such as written below, but again got errors. Would appreciate any suggestions or alternative/efficient code to conduct the t-test (ultimately, I want to make a balance table)