Filter a big matrix
On Wed, Feb 11, 2009 at 1:32 PM, 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
Assuming that I understood your data structure correctly, that all
columns should be tested in your filter, and that exactly one column
should not match the condition, the following should work:
##sample data
X<-matrix(sample(1:200,10000,replace=T),nrow=100)
colnames(X)<-1:100
### Filter function - modify to suit your purpose
filterFunction<-function(n,data){
filteredData<-data[rowSums(data>=199)==1&(data[,n]>=199),,drop=FALSE]
if(nrow(filteredData)==0){
filteredData<-"NoMatchingRows"
}
return(filteredData)
}
names<-colnames(X)
lapply(as.list(names),filterFunction,X)
Hope it helps.
Best regards,
Gustaf
Gustaf Rydevik, M.Sci. tel: +46(0)703 051 451 address:Essingetorget 40,112 66 Stockholm, SE skype:gustaf_rydevik