rpairwise.wilcox.test

Paired samples wilcoxon test over each column


i would like to perform in R a pairwise sample wilcoxon test (by variable "Time") over each column of a dataframe splitted by "Group" (i.e. Group1 and Group2).

structure(list(ID = c(1L, 2L, 3L, 4L, 5L, 10L, 11L, 12L, 13L, 
14L, 1L, 2L, 3L, 4L, 5L, 10L, 11L, 12L, 13L, 14L), Group = c("Group1", 
"Group1", "Group1", "Group1", "Group1", "Group2", "Group2", "Group2", 
"Group2", "Group2", "Group1", "Group1", "Group1", "Group1", "Group1", 
"Group2", "Group2", "Group2", "Group2", "Group2"), Time = c("T0", 
"T0", "T0", "T0", "T0", "T0", "T0", "T0", "T0", "T0", "T2", "T2", 
"T2", "T2", "T2", "T2", "T2", "T2", "T2", "T2"), Alistipes_putredinis = c(20.54997, 
0, 0, 0, 0.00226, 0, 12.83106, 0.38555, 16.45834, 0, 0.6405, 
0, 0, 0, 0, 16.32105, 0, 0, 0, 0), Bidobacterium_l = c(14.43141, 
0.25318, 0.83121, 0.54282, 8.50687, 0.10432, 0.0051, 0.00139, 
0.11766, 0.57905, 0.74302, 0.79018, 0.20329, 5.34884, 1.75612, 
0.25502, 0, 0.60675, 0.59414, 0.85824), Dialister_invisus = c(12.45032, 
7.88459, 0.99032, 1.84241, 0.93828, 1.96771, 5.16734, 3.82884, 
1.55069, 0.97391, 1.43845, 9.77174, 2.59287, 4.70876, 2.30655, 
3.08246, 10.56866, 0.49946, 0.95196, 0.14022), Clostridia_bacterium = c(7.5127, 
0, 0, 0, 0, 6.65269, 0, 0, 4.11219, 0, 5.34908, 0.00794, 0, 0, 
0, 0, 0, 4.33676, 6.2422, 0), Ruminococcus_sp_NSJ_71 = c(6.57903, 
1.45815, 0.28668, 1.66816, 1.66008, 1.85348, 1.80051, 0.91537, 
3.12064, 3.00647, 2.91748, 1.97839, 0.0726, 3.7829, 1.59076, 
1.05453, 0.03881, 3.84824, 0.01241, 2.88977)), class = "data.frame", row.names = c(NA, 
-20L))

Does anyone can help me,please?

Thanks


Solution

  • I'm not entirely certain I understood what you are looking for in terms of the application of the test, but maybe something like this?

    library(dplyr)
    library(tidyr)
    library(purrr)
    library(broom)
    
    myvars <- c("Alistipes_putredinis", "Bidobacterium_l", "Dialister_invisus", "Clostridia_bacterium", "Ruminococcus_sp_NSJ_71")
    
    mydat |> 
      group_by(Group) |> 
      nest() |> 
      mutate(test = map(data, function(dat) map(myvars, function(x) pairwise.wilcox.test(dat[[x]], dat[["Time"]], paired = TRUE) |> 
                                                  tidy() |> 
                                                  mutate(var = x)) |> 
                          list_rbind())) |> 
      unnest(test) |>
      ungroup()
    
    # A tibble: 10 × 6
       Group  data              group1 group2 p.value var                   
       <chr>  <list>            <chr>  <chr>    <dbl> <chr>                 
     1 Group1 <tibble [10 × 7]> T2     T0       0.371 Alistipes_putredinis  
     2 Group1 <tibble [10 × 7]> T2     T0       0.438 Bidobacterium_l       
     3 Group1 <tibble [10 × 7]> T2     T0       0.625 Dialister_invisus     
     4 Group1 <tibble [10 × 7]> T2     T0       1     Clostridia_bacterium  
     5 Group1 <tibble [10 × 7]> T2     T0       1     Ruminococcus_sp_NSJ_71
     6 Group2 <tibble [10 × 7]> T2     T0       0.584 Alistipes_putredinis  
     7 Group2 <tibble [10 × 7]> T2     T0       0.125 Bidobacterium_l       
     8 Group2 <tibble [10 × 7]> T2     T0       1     Dialister_invisus     
     9 Group2 <tibble [10 × 7]> T2     T0       1     Clostridia_bacterium  
    10 Group2 <tibble [10 × 7]> T2     T0       0.438 Ruminococcus_sp_NSJ_71