Reputation: 253
I am iterating through Mann-Whitney tests in R using the following code:
x <- replicate(100, {
sample <- sample_n(foldchange_all_df, 25)
sample_nm <- as.numeric(unlist(sample))
test <- wilcox.test(sample_nm,baselist_nm, exact = FALSE)
})
I get an output of a list with the following output format 100 times:
Wilcoxon rank sum test with continuity correction
data: sample_nm and baselist_nm
W = 209, p-value = 0.04566
alternative hypothesis: true location shift is not equal to 0
I know about the broom package the the function
tidy()
to put this output in a data frame format for a single test.
How can I combine all 100 outputs in the list into a data frame where I can obtain all of the p-values?
Upvotes: 0
Views: 648
Reputation: 28675
If you use simplify = FALSE
in replicate
you get a list, which you can map over with the tidy
function and then rbind. The map_dfr
does both the mapping and rbind-ing in one step.
library(broom)
library(purrr)
out <-
replicate(100, {
wilcox.test(runif(100),runif(100), exact = FALSE)
}, simplify = FALSE) %>%
map_dfr(tidy)
head(out)
#> # A tibble: 6 × 4
#> statistic p.value method alternative
#> <dbl> <dbl> <chr> <chr>
#> 1 5087 0.833 Wilcoxon rank sum test with continuity correcti… two.sided
#> 2 4564 0.287 Wilcoxon rank sum test with continuity correcti… two.sided
#> 3 4063 0.0221 Wilcoxon rank sum test with continuity correcti… two.sided
#> 4 4781 0.593 Wilcoxon rank sum test with continuity correcti… two.sided
#> 5 5751 0.0667 Wilcoxon rank sum test with continuity correcti… two.sided
#> 6 5221 0.590 Wilcoxon rank sum test with continuity correcti… two.sided
Created on 2021-12-21 by the reprex package (v2.0.1)
Upvotes: 1