目标:
我尝试选择包含大量列(> 100k)的NA
/ data.frame
的完整列(没有matrix
)列。
方法:我比较了 dplyr (使用data.frame)和 base (使用矩阵或数据)中获得的性能.frame)但似乎第一种方法(1行dplyr)永远持续,并在停止时出现错误。
示例:,包含50k列
library(dplyr)
# Dummy data
nbcol <- 50000
nbrow <- 5
M <- matrix(runif(nbcol*nbrow), nbrow)
M[M < .01] <- NA # 1% NA
M2 <- as.data.frame(M)
# Dplyr 1 line: 107seconds
system.time({
select(M2, which(apply(M2, 2, function(x) !any(is.na(x)))))
})
# Dplyr 2 lines: 51 seconds
system.time({
colind <- which(apply(M2, 2, function(x) !any(is.na(x))))
select(M2, colind)
})
# Base data.frame: 0.53 seconds
system.time({
M2[,which(apply(M2, 2, function(x) !any(is.na(x))))]
})
# Base matrix: 0.12 seconds
system.time({
M[,which(apply(M, 2, function(x) !any(is.na(x))))]
})
这是预期的结果还是我做错了什么?