# [R] Filter a big matrix

cruz cruadam at gmail.com
Wed Feb 11 13:32:30 CET 2009

```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

```

More information about the R-help mailing list