By running multiple t.test within R (multiple group compared according to multiple quantitative variable), I would like to get something like this within R:
I tried and modified the solution provided here: dplyr summarise multiple columns using t.test
library(dplyr)
library(tidyr)
data(mtcars)
vars_to_test <- c("disp","hp","drat","wt","qsec")
iv <- c("vs", "am")
mtcars %>%
summarise_each_(
funs_(
sprintf("stats::t.test(.[%s == 0], .[%s == 1])$p.value",iv,iv)
),
vars = vars_to_test)
Here is the output:
disp_$..1 hp_$..1 drat_$..1 wt_$..1 qsec_$..1 disp_$..2 hp_$..2 drat_$..2 wt_$..2 qsec_$..2
1 2.476526e-06 1.819806e-06 0.01285342 0.0007281397 3.522404e-06 0.0002300413 0.2209796 5.266742e-06 6.27202e-06 0.2093498
I am facing multiple issues:
- (1) I have two warnings messages that i don't how two solve
Please use summarise_if(), summarise_at(), or summarise_all() instead:
- To map `funs` over all variables, use summarise_all()
- To map `funs` over a selection of variables, use summarise_at()
This warning is displayed once per session.
2: funs_() is deprecated.
Please use list() instead
- (2) How to get one row per group/outcome ?
- (3) How to change the "1" (first character of the second row) by the name of the group (ie : "vs" and "am" in this case)?
- (4) How to export this output as a dataframe (as in my begining example)
Thanks a lot for your help!