rstatix
rstatix copied to clipboard
Cannot inject environmental variable into t_test()
I'm writing a function wherein I have a user-defined grouping variable in my environment that I would like to inject into t_test()
(e.g., t_test({{var}} ~ {{groupvar}})
), but I keep getting an error related to pull(), indicating the column cannot be found. I'm not an expert in data-masking and injection, but I've tried every variation that I can think of and continue to get the same issue.
Here is code to generate a toy dataset and the various approaches I have tried, along with the error messages:
groupvar <- "Treatment"
facetvar <- "Sex"
plotvar <- "BodyMass"
df <- tibble(Subject = as.factor(1:10), BodyMass = runif(10,min = 20, max = 30), Sex = as.factor(rep(c("M","F"),5)), Treatment = as.factor(rep(c("Control","Experimental"),each = 5)))
# This works:
df_ttest <- df %>%
group_by(Sex) %>% # group by variable that will be used to facet
t_test(BodyMass ~ Treatment) %>% #
adjust_pvalue(method = "bonferroni") %>%
add_significance() %>%
add_xy_position(x = "Treatment")
# This breaks:
df_ttest <- df %>%
group_by({{facetvar}}) %>% # group by variable that will be used to facet
t_test({{plotvar}} ~ {{groupvar}}) %>% #
adjust_pvalue(method = "bonferroni") %>%
add_significance() %>%
add_xy_position(x = {{groupvar}})
I've also tried:
-
get()
-
enquo()
and!!
-
.env$var
All give a version of the same error indicating the columns don't exist.
Error in `pull()`:
! Can't extract columns that don't exist.
✖ Column `{\n {\n groupvar\n }\n}` doesn't exist.
R Version: 4.2.2 R Studio Version: 2022.07.2 Build 576 rstatix version: 0.7.1.999
I would really appreciate any suggestions as to where to go from here, as I'm hoping to automatically generate plots with summary statistics for many plots from a large dataset.
Thank you!
@ckfaber, this is not an rstatix
issue so please, close it.
Still, I will help you. Construct a formula via as.formula()
: as.formula(paste(plotvar, "~", groupvar))
Is this what you expect:
library(rstatix)
#>
#> Attaching package: 'rstatix'
#> The following object is masked from 'package:stats':
#>
#> filter
groupvar <- "Treatment"
facetvar <- "Sex"
plotvar <- "BodyMass"
df <- tibble(
Subject = as.factor(1:10),
BodyMass = runif(10,min = 20, max = 30),
Sex = as.factor(rep(c("M","F"),5)),
Treatment = as.factor(rep(c("Control","Experimental"), each = 5))
)
# This works:
df_ttest <- df %>%
group_by(Sex) %>% # group by variable that will be used to facet
t_test(BodyMass ~ Treatment) %>% #
adjust_pvalue(method = "bonferroni") %>%
add_significance() %>%
add_xy_position(x = "Treatment")
df_ttest
#> # A tibble: 2 × 15
#> Sex .y. group1 group2 n1 n2 statistic df p p.adj p.adj.signif
#> <fct> <chr> <chr> <chr> <int> <int> <dbl> <dbl> <dbl> <dbl> <chr>
#> 1 F Body… Contr… Exper… 2 3 1.97 2.48 0.162 0.324 ns
#> 2 M Body… Contr… Exper… 3 2 -0.00905 2.69 0.993 1 ns
#> # ℹ 4 more variables: y.position <dbl>, groups <named list>, xmin <dbl>,
#> # xmax <dbl>
# This breaks:
df_ttest_2 <- df %>%
group_by({{facetvar}}) %>% # group by variable that will be used to facet
t_test(as.formula(paste(plotvar, "~", groupvar))) %>% #
adjust_pvalue(method = "bonferroni") %>%
add_significance() %>%
add_xy_position(x = {{groupvar}})
df_ttest_2
#> # A tibble: 1 × 15
#> `"Sex"` .y. group1 group2 n1 n2 statistic df p p.adj
#> <chr> <chr> <chr> <chr> <int> <int> <dbl> <dbl> <dbl> <dbl>
#> 1 Sex BodyMass Control Experimental 5 5 1.28 7.41 0.239 0.239
#> # ℹ 5 more variables: p.adj.signif <chr>, y.position <dbl>,
#> # groups <named list>, xmin <dbl>, xmax <dbl>
Created on 2023-05-15 with reprex v2.0.2
?