How to melt pairwise.wilcox.test output using dplyr?

后端 未结 2 1226
野性不改
野性不改 2020-12-12 06:52

I want to apply pairwise.wilcox.test for multiple independent variables at a time and then want to have the output in long format. For a particular Wavelength,

2条回答
  •  臣服心动
    2020-12-12 07:21

    I could able to solve the problem using rstatix package which "provides a simple and intuitive pipe friendly framework, coherent with the 'tidyverse' design philosophy for performing basic statistical tests".

    library(tidyverse)
    library(rstatix)
    
    as_tibble(df)%>% 
      pivot_longer(cols = -Class, names_to = "Wavelengths", values_to = "value") %>% 
      mutate(Class = as.factor(Class)) %>% 
      group_by(Wavelengths) %>% 
      pairwise_wilcox_test(value~Class, p.adjust.method="bonf")
    

    which returns the following output

    #> # A tibble: 40 x 10
    #>   Wavelengths .y.   group1 group2    n1    n2 statistic     p p.adj
    #> *                     
    #> 1 WV_350      value 1      2          5     5      20   0.151 1    
    #> 2 WV_350      value 1      3          5     5      25   0.008 0.079
    #> 3 WV_350      value 1      4          5     5      25   0.008 0.079
    #> 4 WV_350      value 1      5          5     5      25   0.008 0.079
    #> 5 WV_350      value 2      3          5     5      25   0.008 0.079
    #> 6 WV_350      value 2      4          5     5      25   0.008 0.079
    #> 7 WV_350      value 2      5          5     5      25   0.008 0.079
    #> 8 WV_350      value 3      4          5     5      10   0.69  1    
    #> 9 WV_350      value 3      5          5     5      21.5 0.075 0.749
    #> 10 WV_350      value 4      5          5     5      22   0.056 0.556
    #> # ... with 30 more rows, and 1 more variable: p.adj.signif 
    

提交回复
热议问题