matrix - How to do multiple wilcox.test in R? -
i have matrix , aim wilxcon test in r (controls vs cases) not sure how put in matrix properly. thanks
gene.name cont1 cont2 cont3 case1 case2 case3 10 2 3 21 18 8 b 14 8 7 12 34 22 c 16 9 19 21 2 8 d 32 81 17 29 43 25 ..
you can try:
# load data d <- read.table(text="gene.name cont1 cont2 cont3 case1 case2 case3 10 2 3 21 18 8 b 14 8 7 12 34 22 c 16 9 19 21 2 8 b 32 81 17 29 43 25", header=t) library(tidyverse) # transform long format using dplyr (included in tidyverse) dlong <- as.tbl(d) %>% gather(key, value,-gene.name) %>% mutate(group=ifelse(grepl("cont",key), "control", "case")) # plot data dlong %>% ggplot(aes(x=group, y=value)) + geom_boxplot()
# run test dlong %>% with(., wilcox.test(value ~ group)) wilcoxon rank sum test continuity correction data: value group w = 94.5, p-value = 0.2034 alternative hypothesis: true location shift not equal 0
edits
# don't clarified how handle double occurence of b assume # thats typo , fixed second b d library(ggpubr) dlong <- as.tbl(d) %>% mutate(gene.name=letters[1:4]) %>% gather(key, value,-gene.name) %>% mutate(group=ifelse(grepl("cont",key), "control", "case")) # plot boxplot wilcoxen p-values using ggpubr dlong %>% ggplot(aes(x=gene.name, y=value, fill=group)) + geom_boxplot() + stat_compare_means(method= "wilcox.test")
# pvalues dlong %>% group_by(gene.name) %>% summarise(p=wilcox.test(value~group)$p.value) # tibble: 4 x 2 gene.name p <chr> <dbl> 1 0.2 2 b 0.2 3 c 0.7 4 d 1.0
or try base r using apply.
res <- apply(d[,-1], 1, function(x){ wilcox.test(x ~ c(1,1,1,2,2,2))$p.value }) cbind.data.frame(genes=as.character(d$gene.name), p=res, bh=p.adjust(res, method = "bh")) genes p bh [1,] 1 0.2 0.4000000 [2,] 2 0.2 0.4000000 [3,] 3 0.7 0.9333333 [4,] 2 1.0 1.0000000
Comments
Post a Comment