如何在R中进行多重wilcox.test?

如何在R中进行多重wilcox.test?,r,matrix,R,Matrix,我有这个矩阵,目的是在R(对照组与病例组)中进行Wilcoxon测试,但我不确定如何正确地放入矩阵中 gene.name cont1 cont2 cont3 case1 case2 case3 A 10 2 3 21 18 8 B 14 8 7 12 34 22 C 16 9 19 21 2 8

我有这个矩阵,目的是在R(对照组与病例组)中进行Wilcoxon测试,但我不确定如何正确地放入矩阵中

gene.name  cont1 cont2  cont3  case1  case2  case3
A           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
..
您可以尝试:

# load your data 
d <- read.table(text="gene.name  cont1 cont2  cont3  case1  case2  case3
A           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 to 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 the data
dlong %>% 
  ggplot(aes(x=group, y=value)) +
   geom_boxplot()
编辑
#由于您没有阐明如何处理B的双重出现,我想
#这是一个打字错误,并将第二个B改为D
图书馆(ggpubr)
德隆%
突变(基因名称=字母[1:4])%>%
聚集(键,值,-gene.name)%%>%
变异(组=ifelse(grepl(“cont”,key),“control”,“case”))
#使用ggpubr使用Wilcoxen p值绘制箱线图
dlong%>%
ggplot(aes(x=gene.name,y=value,fill=group))+
geom_箱线图()+
统计比较平均值(方法=“wilcox.test”)

#获取pvalues
dlong%>%
分组依据(基因名称)%>%
总结(p=wilcox.test(value~group)$p.value)
#一个tibble:4x2
基因名
1 A 0.2
2b0.2
3 C 0.7
4d1.0
或者使用apply尝试base R

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

res您尝试了什么?可能会有帮助。我以前做过,但那没有测试,但是我需要做Wilxcon测试,因为我的数据是非参数的Hanks Jimbou。但是我需要一个基因列表,每个基因都有P值(一行一行)。或者获取通过P值的基因列表,请有人帮忙!我想要的只是一个简单的U型测试,但我不知道如何放入我的矩阵???@Behmah首先你必须解释为什么B出现两次,以及如何处理它?作为独立的测量或重复的测量,或作为自己的组?它工作得很好。关于第二个解决方案(base R apply),我如何写出结果和基因名称(我有一个大的基因列表)?
# as you don't clarified how to handle the double occurence of B I assume 
# thats a typo and fixed the second B to 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 the boxplot with Wilcoxen p-values using ggpubr
dlong %>% 
  ggplot(aes(x=gene.name, y=value, fill=group)) +
  geom_boxplot() +
  stat_compare_means(method= "wilcox.test")
# get the pvalues
dlong %>% 
  group_by(gene.name) %>% 
  summarise(p=wilcox.test(value~group)$p.value)
# A tibble: 4 x 2
   gene.name     p
       <chr> <dbl>
1         A   0.2
2         B   0.2
3         C   0.7
4         D   1.0
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