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:
Hi,
I have a big matrix X with M rows and N columns that I want to filter
it into smaller ones with m (<M) rows and N columns.
The filter rule is based on the values of each columns, i.e.
X looks like this:
column name: a, b, c, d, ... etc
a b c d ...
1 2 3 4 ...
5 6 7 8 ...
9 8 7 6 ...
... ... ... ...
The filter rule with the result that I want is:
X[X$a<5 & X$b<5 & X$c<5 & X$d<5 ...etc ,]
X[X$a<5 & X$b<5 & X$c<5 & X$d>=5 ...etc ,]
X[X$a<5 & X$b<5 & X$c>=5 & X$d<5 ...etc ,]
... ... ...
...
with all the possible combinations which is 2^M
I try to use multiple for loops to separate it:
for (i in 1:2)
for (j in 1:2)
for (k in 1:2)
... ...
assign(paste(i,j,k,...,sep="")), X[if (i==1) paste("X$a<5")
else paste("X$a>=5") & if (i==1) paste("X$b<5") else paste("X$b>=5") &
..., ])
# there might be syntax errors, I just want to clearly describe my problem
Since paste("X$a>=5") gives type of character; whereas the type of
X$a>=5 should be logical.
How can I do this?
All thoughts are greatly appreciated.
Many Thanks,
cruz
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?