比较同一组内的列

Comparing columns within the same group

我的数据框:

data <- structure(list(group = c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), col1 = c(9, 
9.05, 7.15, 7.21, 7.34, 8.12, 7.5, 7.84, 7.8, 7.52, 8.84, 6.98, 
6.1, 6.89, 6.5, 7.5, 7.8, 5.5, 6.61, 7.65, 7.68), col2 = c(11L, 
11L, 10L, 1L, 3L, 7L, 11L, 11L, 11L, 11L, 4L, 1L, 1L, 1L, 2L, 
2L, 1L, 4L, 8L, 8L, 1L), col3 = c(7L, 11L, 3L, 7L, 11L, 2L, 11L, 
5L, 11L, 11L, 5L, 11L, 11L, 2L, 9L, 9L, 3L, 8L, 11L, 11L, 2L), 
    col4 = c(11L, 11L, 11L, 11L, 6L, 11L, 11L, 11L, 10L, 7L, 
    11L, 2L, 11L, 3L, 11L, 11L, 6L, 11L, 1L, 11L, 11L), col5 = c(11L, 
    1L, 2L, 2L, 11L, 11L, 1L, 10L, 2L, 11L, 1L, 3L, 11L, 11L, 
    8L, 8L, 11L, 11L, 11L, 2L, 9L)), class = "data.frame", row.names = c(NA, 
-21L))

我有一个函数可以比较每组中的列。我希望它不是将所有列相互比较,而是只比较那些我 select.

现在函数比较:(2-3;2-4;2-5;2-6;3-4;3-5;3-6;4-5;4-6;5-6 )

我想自己设置这个顺序,例如:(2-4;3-5;4-6)

函数:

wilcox.fun <- function(dat) { 
  do.call(rbind, combn(names(dat), 2, function(x) {
    test <- wilcox.test(dat[[x[1]]], dat[[x[2]]], paired=TRUE)
    data.frame(Test = sprintf('Group %s by Group %s', x[1], x[2]), 
               W = round(test$statistic,4), 
               p = test$p.value)
  }, simplify = FALSE))
}


result <- purrr::map_df(split(data[,c(2,3,4,5,6)], data$group), wilcox.fun, .id = 'Group')

您可以创建包含您感兴趣的组合的向量列表。将计算每个组合的函数中的 combn 更改为 lapply,这将仅针对我们计算的特定组合进行计算定义并将结果与​​ purrr::map_df.

组合
combination <- list(c(2, 4), c(3, 5), c(4, 6))

wilcox.fun <- function(dat) { 
  do.call(rbind, lapply(combination, function(x) {
    test <- wilcox.test(dat[[x[1]]], dat[[x[2]]], paired=TRUE)
    data.frame(Test = sprintf('Group %s by Group %s', x[1], x[2]), 
               W = round(test$statistic,4), 
               p = test$p.value)
  }))
}

result <- purrr::map_df(split(data, data$group), wilcox.fun, .id = 'Group')