如何执行逐列wilcox



我有data.framedf1:

set.seed(12345)
df1 <- data.frame(group=c(rep("apple", 4), rep("pear",6)), a=rnorm(10,0,0.4), 
b=rnorm(10,0,0.2), 
c=rnorm(10,0,0.7), d=rnorm(10,0,0.9), e=rnorm(10,0,0.5))

如何获得按列的wilcox。测试比较苹果(第1:4行)和梨(第5:10行)的p值,并将该p值添加到底部的新行,结果为df2:

df2 <- data.frame(group=c(rep("apple", 4), rep("pear",6), "wilcox.test"), 
a=c(rnorm(10,0,0.4), 0.393768635), b=c(rnorm(10,0,0.2), 0.286422023), 
c=c(rnorm(10,0,0.7), 1), d=c(rnorm(10,0,0.9), 0.033006258), 
e=c(rnorm(10,0,0.5), 1))

> df2
group   a   b   c   d   e
1  apple   0.23421153  -0.02324956 0.5457353   0.73068586  0.5642554
2  apple   0.28378641  0.36346241  1.0190496   1.97715019  -1.190179
3  apple   -0.04372133 0.07412557  -0.4510299  1.8442713   -0.5301328
4  apple   -0.18139887 0.10404329  -1.0871962  1.46920108  0.4685703
5  pear    0.24235498  -0.1501064  -1.1183967  0.22884407  0.4272259
6  pear    -0.72718239 0.16337997  1.2635683   0.44206945  0.7303647
7  pear    0.25203942  -0.1772715  -0.3371532  -0.29167792 -0.7065494
8  pear    -0.11047364 -0.06631552 0.4342659   -1.49584522 0.2837016
9  pear    -0.1136639  0.22414253  0.4284864   1.59096047  0.2915938
10 pear    -0.3677288  0.05974474  -0.1136177  0.02322094  -0.6533994
11 wilcox.test 0.393768635 0.286422023 1   0.033006258 1

这是一个基本R选项-

subset取其中两个值的数据,用Map对每一列应用wilcox.test,从中提取p值,作为新行添加到已有的df1中。

rbind(df1, data.frame(group = 'wilcox.test', 
mapply(function(x, y) wilcox.test(x, y)$p.value, 
subset(df1, group == 'apple', select = -group),
subset(df1, group == 'pear', select = -group)) |>
t() |> data.frame()))
#         group           a           b          c           d          e
#1        apple  0.23421153 -0.02324956  0.5457353  0.73068586  0.5642554
#2        apple  0.28378641  0.36346241  1.0190496  1.97715019 -1.1901790
#3        apple -0.04372133  0.07412557 -0.4510299  1.84427130 -0.5301328
#4        apple -0.18139887  0.10404329 -1.0871962  1.46920108  0.4685703
#5         pear  0.24235498 -0.15010640 -1.1183967  0.22884407  0.4272259
#6         pear -0.72718239  0.16337997  1.2635683  0.44206945  0.7303647
#7         pear  0.25203942 -0.17727150 -0.3371532 -0.29167792 -0.7065494
#8         pear -0.11047364 -0.06631552  0.4342659 -1.49584522  0.2837016
#9         pear -0.11366390  0.22414253  0.4284864  1.59096047  0.2915938
#10        pear -0.36772880  0.05974474 -0.1136177  0.02322094 -0.6533994
#11 wilcox.test  0.47619048  0.35238095  1.0000000  0.03809524  1.0000000

使用r4.1中的管道(|>)以提高可读性。

我们可以用dplyr来做。我们可以用wilcox.test来表示summarise,用$来表示p.value,然后用bind_rows来绑定p值和调整后的p值作为最后一行。

df1 %>% summarise(across(!group, ~wilcox.test(.x ~ group)$p.value)) %>%
bind_rows(., p.adjust(., method = 'bonferroni')) %>%
bind_rows(df1, .) %>%
mutate(group=replace(group, is.na(group), c('p.values', 'adjusted_p.values')))
group           a           b          c           d          e
1              apple  0.23421153 -0.02324956  0.5457353  0.73068586  0.5642554
2              apple  0.28378641  0.36346241  1.0190496  1.97715019 -1.1901790
3              apple -0.04372133  0.07412557 -0.4510299  1.84427130 -0.5301328
4              apple -0.18139887  0.10404329 -1.0871962  1.46920108  0.4685703
5               pear  0.24235498 -0.15010640 -1.1183967  0.22884407  0.4272259
6               pear -0.72718239  0.16337997  1.2635683  0.44206945  0.7303647
7               pear  0.25203942 -0.17727150 -0.3371532 -0.29167792 -0.7065494
8               pear -0.11047364 -0.06631552  0.4342659 -1.49584522  0.2837016
9               pear -0.11366390  0.22414253  0.4284864  1.59096047  0.2915938
10              pear -0.36772880  0.05974474 -0.1136177  0.02322094 -0.6533994
11          p.values  0.47619048  0.35238095  1.0000000  0.03809524  1.0000000
12 adjusted_p.values  1.00000000  1.00000000  1.0000000  0.19047619  1.0000000

相关内容

  • 没有找到相关文章

最新更新