Let's say I have mtcars
dataset with columns mpg
and cyl
.
mpg cyl
21.0 6
21.0 6
22.8 4
21.4 6
18.7 8
18.1 6
I would like to calculate all t.test()
(or wilcox.test()
) statistics between group where cyl == 4
and others groups. Results should be a tibble that looks like:
mpg_4 <- mtcars %>% filter(cyl == 4) %>% select(mpg)
mpg_6 <- mtcars %>% filter(cyl == 6) %>% select(mpg)
mpg_8 <- mtcars %>% filter(cyl == 8) %>% select(mpg)
bind_rows(
broom::tidy(t.test(mpg_4, mpg_4)),
broom::tidy(t.test(mpg_4, mpg_6)),
broom::tidy(t.test(mpg_4, mpg_)
)
I would like to do it using it purrr
and broom
unless there's a cleaner way. Please note that it should work for n groups and it should be applicable to easily changed to a different test.