我的数据框:
data<-structure(list(GROUP = c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L),
col1 = c(23L, 432L, 234L, 234L, 3123L, 657L,
8768L, 123L,42323L),
col2 = c(567L, 765L, 8678L, 46L, 35L, 24L, 76L,
789L, 45L),
col3 = c(1L, 3L, 5L, 7L, 8L, 0L, 8L, 7L, 3L),
col4 = c("S", "S", "S", "S", "F", "F", "F",
"F", "F"),
col5 = c("a","a", "s", "s", "s", "s", "s",
"s", "s")
), class = "data.frame", row.names = c(NA,-9L))
功能:
combination = list(c(2,3),c(3,4),c(5,6))
wilcox.fun <- function(df, id_group){
df = df[df$GROUP%in%id_group,]
x <- function(dat) {
do.call(rbind, lapply(combination, function(x) {
col1 <- dat[[x[1]]]
col2 <- dat[[x[2]]]
if(is.numeric(col1) && is.numeric(col2)) test <- wilcox.test(col1, col2)
else test <- fisher.test(col1, col2)
data.frame(Test = sprintf('Group %s by Group %s Group',x[1],x[2]),
p = test$p.value,
col <- paste(x[1],unique(dat[[x[1]]]),unique(table(dat[[x[1]]])),":",x[2],unique(dat[[x[2]]]),unique(table(dat[[x[2]]])))
)
}))
}
return (purrr::map_df(split(df, df$GROUP),x,.id="GROUP" ))
}
wilcox.fun(data, c(1, 2))
我想按组计算列中的每个值。
例如,对于列col4
,第1组中“S”的数量=2。第2组中,“S”的数量=2。第1组中,“F”的数量=3。第2组中, “F”的数量 = 2 依此类推
我尝试使用该table
函数,但它输出了太多重复组合。
你可以试试下面的代码
lapply(
data[-1],
aggregate,
data[1],
length
)
这使
$col1
GROUP x
1 1 5
2 2 4
$col2
GROUP x
1 1 5
2 2 4
$col3
GROUP x
1 1 5
2 2 4
$col4
GROUP x
1 1 5
2 2 4
$col5
GROUP x
1 1 5
2 2 4
或使用 table
lapply(
data[-1],
table,
data$GROUP
)
给
$col1
1 2
23 1 0
123 0 1
234 1 1
432 0 1
657 0 1
3123 1 0
8768 1 0
42323 1 0
$col2
1 2
24 0 1
35 1 0
45 1 0
46 0 1
76 1 0
567 1 0
765 0 1
789 0 1
8678 1 0
$col3
1 2
0 0 1
1 1 0
3 1 1
5 1 0
7 0 2
8 2 0
$col4
1 2
F 3 2
S 2 2
$col5
1 2
a 1 1
s 4 3
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句