如何对 R 中的组按列执行 wilcox.test

How to perform column-wise wilcox.test on groups in R

我有 data.frame df1:

 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))

如何获得比较苹果(行 1:4)和梨(行 5:10)的列 wilcox.test 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

使用 R 4.1 中的管道 (|>) 以提高可读性。

我们可以用 dplyr 来做。我们可以用summarisewilcox.test,提取p.value$,然后用bind_rows绑定p.values并调整p.values作为最后一行。

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