Ian
Ian

Reputation: 89

Group t test result into columns within tidyverse

I'd like to group multiple t test result into one table. Originally my code looks like this:

tt_data <- iris %>% 
            group_by(Species) %>%
            summarise(p = t.test(Sepal.Length,Petal.Length,alternative="two.sided",paired=T)$p.value,
                estimate = t.test(Sepal.Length,Petal.Length,alternative="two.sided",paired=T)$estimate
            )

tt_data
# Species    p              estimate
# setosa     2.542887e-51   3.544
# versicolor 9.667914e-36   1.676
# virginica  7.985259e-28   1.036

However, base on the idea that I should only perform the statistical test once, is there a way for me to run t test once per group and collect the intended table? I think there are some combination of broom and purrr but I am unfamiliar with the syntax.

# code idea (I know this won't work!)
tt_data <- iris %>% 
            group_by(Species) %>%
            summarise(tt = t.test(Sepal.Length,Petal.Length,alternative="two.sided",paired=T)) %>%
            select(Species, tt.p, tt.estimate)

tt_data
# Species    tt.p           tt.estimate
# setosa     2.542887e-51   3.544
# versicolor 9.667914e-36   1.676
# virginica  7.985259e-28   1.036

Upvotes: 3

Views: 1259

Answers (2)

Sotos
Sotos

Reputation: 51582

You can use map to select the desired values from the list generated by t.test and by tidying it up to a data frame via broom::tidy, i.e.

library(dplyr)

iris %>%
  group_by(Species) %>%
  summarise(p = list(broom::tidy(t.test(Sepal.Length, Petal.Length, alternative = "two.sided", paired = T)))) %>% 
  mutate(p.value = purrr::map(p, ~select(.x, c('p.value', 'estimate')))) %>% 
  select(-p) %>% 
  unnest()


# A tibble: 3 x 3
#  Species     p.value estimate
#  <fct>         <dbl>    <dbl>
#1 setosa     2.54e-51     3.54
#2 versicolor 9.67e-36     1.68
#3 virginica  7.99e-28     1.04

Upvotes: 1

GGamba
GGamba

Reputation: 13680

You can use broom::tidy() to transform the resut of the t.test to a tidy 'tibble':

library(dplyr)
library(broom)

iris %>% 
  group_by(Species) %>%
  group_modify(~{
    t.test(.$Sepal.Length,.$Petal.Length,alternative="two.sided",paired=T) %>% 
      tidy()
  }) %>% 
  select(estimate, p.value)

#> Adding missing grouping variables: `Species`
#> # A tibble: 3 x 3
#> # Groups:   Species [3]
#>   Species    estimate  p.value
#>   <fct>         <dbl>    <dbl>
#> 1 setosa         3.54 2.54e-51
#> 2 versicolor     1.68 9.67e-36
#> 3 virginica      1.04 7.99e-28

Created on 2020-09-02 by the reprex package (v0.3.0)

Upvotes: 3

Related Questions