0
votes

I have a sample dataset as below:

Day<-c(1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2)
Group<-c("A","A","A","B","B","B","C","C","C","A","A","A","A","B","B","B","C","C","C")
Rain<-c(4,4,6,5,3,4,5,5,3,6,6,6,5,3,3,3,2,5,2)
UV<-c(6,6,7,8,5,6,5,6,6,6,7,7,8,8,5,6,8,5,7)

I ran Kruskal Wallis test on the dataset:

library(rstatix)
library(tidyverse)

cols <- c('Rain', 'UV')
map_df(cols, ~dat %>% group_by(Day) %>% kruskal_test(reformulate('Group', .x)))

Following Kruskal Wallis results, how can I incorporate this: to run pairwise.wilcox.test if p value from Kruskal Wallis is <0.05, then to print out the results in a data frame with the groups tested and the adjusted p values? Thanks.

1

1 Answers

1
votes

You could subset the columns that have p-value less than 0.05 and apply wilcox_test to only those columns.

library(tidyverse)
library(rstatix)

result <- map_df(cols, ~dat %>% 
                        group_by(Day) %>% 
                        kruskal_test(reformulate('Group', .x)))

group <- unique(result$.y.[result$p < 0.05])
map_df(group, ~dat %>% group_by(Day) %>% wilcox_test(reformulate('Group', .x)))

#    Day .y.   group1 group2    n1    n2 statistic     p p.adj p.adj.signif
#  <dbl> <chr> <chr>  <chr>  <int> <int>     <dbl> <dbl> <dbl> <chr>       
#1     1 Rain  A      B          3     3       6   0.643 1     ns          
#2     1 Rain  A      C          3     3       5   1     1     ns          
#3     1 Rain  B      C          3     3       3.5 0.814 1     ns          
#4     2 Rain  A      B          4     3      12   0.036 0.107 ns          
#5     2 Rain  A      C          4     3      11.5 0.061 0.123 ns          
#6     2 Rain  B      C          3     3       6   0.637 0.637 ns