Search code examples
ranova

How is have to be a dataset to perform an ANOVA test in R?


I have three columns, one per group, with numeric values. I want to analyze them using an Anova test, but I found applications when you have the different groups in a column and the respective values in the second column. I wonder if it is necessary to reorder the data like that, or if there is a method that I can use for the columns that I currently have. Here I attached a capture:

enter image description here

Thanks!


Solution

  • You can convert a wide table having many columns into another table having only two columns for key (group) and value (response) by pivoting the data:

    library(tidyverse)
    
    # create example data
    set.seed(1337)
    data <- tibble(
      VIH = runif(100),
      VIH2 = runif(100),
      VIH3 = runif(100)
    )
    data
    #> # A tibble: 100 × 3
    #>       VIH   VIH2   VIH3
    #>     <dbl>  <dbl>  <dbl>
    #>  1 0.576  0.485  0.583 
    #>  2 0.565  0.495  0.108 
    #>  3 0.0740 0.868  0.350 
    #>  4 0.454  0.833  0.324 
    #>  5 0.373  0.242  0.915 
    #>  6 0.331  0.0694 0.0790
    #>  7 0.948  0.130  0.563 
    #>  8 0.281  0.122  0.287 
    #>  9 0.245  0.270  0.419 
    #> 10 0.146  0.488  0.838 
    #> # … with 90 more rows
    
    data %>%
      pivot_longer(everything()) %>%
      aov(value ~ name, data = .)
    #> Call:
    #>    aov(formula = value ~ name, data = .)
    #> 
    #> Terms:
    #>                      name Residuals
    #> Sum of Squares   0.124558 25.171730
    #> Deg. of Freedom         2       297
    #> 
    #> Residual standard error: 0.2911242
    #> Estimated effects may be unbalanced
    

    Created on 2022-05-10 by the reprex package (v2.0.0)