我的数据框架:
data <-structure(list(col1 = c(125L, 654L, 896L, 154L, 865L, 148L),
col2 = c(489L, 657L, 198L, 269L, 789L, 456L), col3 = c(741L,
852L, 963L, 987L, 951L, 632L), col4 = c(124L, 785L, 874L,
965L, 563L, 145L), col5 = c(963L, 146L, 259L, 367L, 365L,
189L), col6 = c(741L, 777L, 100L, 200L, 956L, 452L), col7 = c(456L,
666L, 300L, 778L, 888L, 999L), col8 = c(254L, 732L, 400L,
500L, 600L, 700L), col9 = c(555L, 638L, 127L, 489L, 545L,
54L), col10 = c(921L, 549L, 111L, 222L, 354L, 355L), GROUP = c(1L,
2L, 3L, 1L, 2L, 3L)), class = "data.frame", row.names = c(NA,
-6L))
我的乐趣:
combination <- list(c(1,2),c(1,3),c(2,3),c(4,5),c(4,6),c(5,6),c(7,8),c(7,9),c(8,9))
wilcox.fun <- function(df, id_group){
df = df[df$GROUP%in%id_group,]
x <- function(dat) {
do.call(rbind, Map(function(x, y) {
col1 <- dat[[x[1]]]
col2 <- dat[[x[2]]]
test <- wilcox.test(col1, col2,conf.int = TRUE)
print("work")
median.group.1 <- median((dat[[x[1]]]))
median.group.2 <- median((dat[[x[2]]]))
diff.1 <- -round(test$estimate, 2)
data.frame(NAME = sprintf('Group %s by Group %s',x[1],x[2]),
stats=paste(x[1],":",median.group.1,":",x[2],median.group.2),
diff=paste(x[1],"-",x[2],diff.1,collapse = "\n")
)
}, combination))
}
return (purrr::map_df(split(data, data$GROUP),x, .id ="GROUP"))
}
result <- wilcox.fun(data, c("1","2"))
names(result)[3] <- "stats"
names(result)[4] <- "diff"
我希望函数以这种格式输出数据:
|GROU| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | dif |
|------|----|-----|-----|-----|-----|---|---|-----|----------------| |
|1 | 139.5| 379|864 |544.5|665 |470.5|617|377|522 | 1 - 2 239.5 |
| | | 1 - 3 724.5 |
| | | 2 - 3 485 |
| | | 4 - 5 120.5 |
| | | etc |
|--- |------|----|-----|-----|-----|-----|---|---|-----|----------------|
|2 |759.5 |723 |901.5|674 |255.5|866.5|777|666|591.5|1 - 2 -36.5 | |
| | |1 - 3 142 | |
| | |2 - 3 178.5 | |
| | |4 - 5 -418.5 | |
| | | etc | |
也就是说,我希望形成一个数据框,其中中位数记录在每个新列中而不重复,并且中位数的所有比较都在一列中。我不太明白如何形成这样的数据框