Search code examples
rdplyrstatisticsanovat-test

Running multiply Anova and/or T-Test on variables in one dataframe in R


I've the a data frame containing different items (and it's cost) and also it's subsequent groupings. I would like to run an Anova and/or T-Test for each item based on their groupings to see if their mean differs. Anybody knows how to do this in R?

A sample of the dataframe is as follow:

Item Cost Grouping
Book A 7 A
Book A 9 B
Book A 6 A
Book A 7 B
Book B 4 A
Book B 6 B
Book B 5 A
Book B 3 C
Book C 5 C
Book C 4 A
Book C 7 C
Book C 2 B
Book C 2 B
Book D 4 A
Book D 2 C
Book D 9 C
Book D 4 A

The output should be a simple table (or any similar table) as follows

Item P-Value (from ANOVA/t-test) (H0: Mean same for all groupings)
Book A xxx
Book B xxx
Book C xxx
Book D xxx

Thanks in advance!


Solution

  • You could use anova_test from the rstatix package like this:

    df <- data.frame(Item = c("Book A", "Book A", "Book A", "Book A", "Book B", "Book B", "Book B", "Book B"),
                     Cost = c(7,9,6,7,4,6,5,3),
                     Grouping = c("A", "B", "A", "B", "A", "B", "A", "C"))
    
    library(dplyr)
    library(rstatix)
    df %>% 
      group_by(Item) %>%
      anova_test(Cost ~ Grouping)
    #> Coefficient covariances computed by hccm()
    #> Coefficient covariances computed by hccm()
    #> # A tibble: 2 × 8
    #>   Item   Effect     DFn   DFd     F     p `p<.05`   ges
    #> * <chr>  <chr>    <dbl> <dbl> <dbl> <dbl> <chr>   <dbl>
    #> 1 Book A Grouping     1     2   1.8 0.312 ""      0.474
    #> 2 Book B Grouping     2     1   4.5 0.316 ""      0.9
    

    Created on 2022-07-10 by the reprex package (v2.0.1)