Re: [R] efficient filtering of matrices

From: Uwe Ligges <ligges_at_statistik.uni-dortmund.de>
Date: Fri 06 May 2005 - 04:18:19 EST

sms13+@pitt.edu wrote:

> I was wondering if someone can tell me the best way to search through a
> matrix and choose certain rows (based on certain conditions) to put into
> a separate matrix.
> What I have tried so far is very slow for a large dataset I'm working
> with. e.g., I have this piece of code to create a new matrix (newmat)
> based on my filtering conditions. Do I need to do this kind of thing
> where I keep rbinding?
>
> newmat<-rep(NA,12)
> for (i in 1:length(origmat[,1])
> {
> if ( is.na(origmat[i,10]) | (!is.na(origmat[i,10]) &
> (origmat[i,2] <= origmat[i,10]) ) )
> newmat<-rbind(newmat, origmat[i,])
> }

What about vectorizing the stuff?

   index <- is.na(origmat[ ,10]) |

     (!is.na(origmat[ ,10]) &
       (origmat[ ,2] <= origmat[ ,10]))

   newmat <- origmat[index,]

Uwe Ligges

>
> Thanks,
> Steven
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Fri May 06 04:25:18 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:31:36 EST