我有一个大数据框架,上面有1000列。其中很少有人是班级
integer
,很少有班级character
,我想在所有列上运行fisher test
,并且尝试了几种方法,但是执行从未通过,最终我不得不中止R Studio。如何做?
我正在尝试遵循方式。
com <- data.frame(t(combn(colnames(MIBCMETAdf), 2)))
head(com)
fisher_tests <- apply(com, 1, function(i) {
tryCatch(fisher.test(table(MIBCMETAdf[, i])), error=function(e) NA_real_)
})
E_table <- cbind(com, t(sapply(fisher_tests, "[", c("p.value", "estimate"))))
head(E_table)
colnames(E_table)
combn
,尝试
RcppAlgos::comboGeneral
。
> tictoc::tic()
> r <- RcppAlgos::comboGeneral(names(MIBCMETAdf), 2, FUN=\(j, ...) {
+ unlist(fisher.test(table(MIBCMETAdf[, j]))[c("p.value", "estimate")])
+ }) |> do.call(what='rbind')
> tictoc::toc()
148.691 sec elapsed
> head(r)
p.value estimate.odds ratio
[1,] 0.8437265 0.9174655
[2,] 0.1685111 1.7551113
[3,] 0.4261868 0.7149415
[4,] 0.5556475 1.2732325
[5,] 0.3126351 0.6215150
[6,] 0.4205574 1.4820226
您可以稍后添加
t(combn(colnames(MIBCMETAdf), 2))
列。
data:
m <- 100; n <- 1000
MIBCMETAdf <- as.data.frame(matrix(sample.int(2, m*n, replace=TRUE), ncol=n))