Skip to content
Prev 170091 / 398506 Next

Filter a big matrix

Try something like this.  This setups the "<" condition and then uses
'xor' to flip it to ">=" assuming that you are using the same values

# test data
n <- 5  # number of columns
m <- 100  # number of rows
x <- matrix(sample(1:10, n * m, TRUE), ncol=n, nrow=m)
# create tests assuming you want to know if things are greater than or
less than a value
test <- apply(x, 2, "<", 5)
# setup all combinations to test for
comb <- expand.grid(rep(list(c(TRUE, FALSE)), n))
result <- lapply(seq(nrow(comb)), function(.row){
    # apply the test using 'xor' to change the conditions from "<" to ">="
    x[apply(test, 1, function(z) all(xor(z, comb[.row,]))),,drop=FALSE]
})
On Wed, Feb 11, 2009 at 7:32 AM, cruz <cruadam at gmail.com> wrote: