[R] How to optimize this codes ?

Daren Tan
Thu, 04 Dec 2008 16:02:49 -0000

How to optimize the for-loop to be reasonably fast for sample.size=100000000 ?
You may want to change sample.size=1000 to have an idea what I am achieving.


set.seed(143)
A <- matrix(sample(0:1, sample.size, TRUE), ncol=10, dimnames=list(NULL,
LETTERS[1:10]))

B <- list()
for(i in 1:10) {
  B[[i]] <- apply(combn(LETTERS[1:10], i), 2, function(x) {
sum(apply(data.frame(A[,x]), 1, all)) })
}

pdf("test.pdf")
boxplot(B, xlab = "Number of predictors", ylab = "Number of
agreements across predictors")
dev.off()