一种方法是在感兴趣的列上循环,作为
split
,用
reformulate
并应用
wilcox.test
out <- lapply(split(df, df$skul), function(x)
lapply(setNames(c("x1", "x2"), c("x1", "x2")), function(y)
wilcox.test(reformulate("i_f", response = y), data = x)))
-输出
> out$a
$x1
Wilcoxon rank sum test with continuity correction
data: x1 by i_f
W = 452, p-value = 0.9822
alternative hypothesis: true location shift is not equal to 0
$x2
Wilcoxon rank sum test with continuity correction
data: x2 by i_f
W = 404.5, p-value = 0.5027
alternative hypothesis: true location shift is not equal to 0
如果我们想使用
tidyverse
library(dplyr)
df %>%
group_by(skul) %>%
summarise(across(c(x1, x2),
~list(broom::tidy(wilcox.test(reformulate("i_f", cur_column()))))))