Peter Wilkinson
2006-Apr-01 23:52 UTC
[R] Using vectorization instead of for loop for performing a calculation efficiently
I am trying to write an efficient function that will do the following: Given an nxm matrix, 10 rows (observations) by 10 columns (samples) for each row, test of all values in the row are greater than a value k If all values are greater than k, then set all values to NA (or something), Return an nxm matrix with the modified rows. If I do this with a matrix of 20,000 rows, I will be waiting until Christmas for it to finish: For rows in Matrix: if rows < filter set all elements in rows to NA (or something) else do nothing Return the matrix with the modified rows I don?t know how to code this properly. The following: If (sum(ifelse(nvector>filter,1,0) == 0 ) ) Tells me if any row has at least 1 value above the filter. How do I get rid of the 'outer' loop? Peter -----------------------------------------> Peter Wilkinson Senior Bioinformatician / Programmer-Analyst National Immune Monitoring Laboratory tel: (514)-343-7876
Gabor Grothendieck
2006-Apr-02 00:58 UTC
[R] Using vectorization instead of for loop for performing a calculation efficiently
Try this:> set.seed(1) > mat <- matrix(rnorm(20000 * 10), 20000) > system.time(mat2 <- replace(mat, rowSums(mat > 0) == 10, NA))[1] 0.04 0.01 0.05 NA NA> R.version.string # Windows XP[1] "R version 2.2.1, 2005-12-20" On 4/1/06, Peter Wilkinson <pwilkinson at videotron.ca> wrote:> I am trying to write an efficient function that will do the following: > > Given an nxm matrix, 10 rows (observations) by 10 columns (samples) > for each row, test of all values in the row are greater than a value k > If all values are greater than k, then set all values to NA (or something), > Return an nxm matrix with the modified rows. > > If I do this with a matrix of 20,000 rows, I will be waiting until Christmas > for it to finish: > > For rows in Matrix: > if rows < filter > set all elements in rows to NA (or something) > else > do nothing > Return the matrix with the modified rows > > > I don't know how to code this properly. The following: > > If (sum(ifelse(nvector>filter,1,0) == 0 ) ) > > Tells me if any row has at least 1 value above the filter. How do I get rid > of the 'outer' loop? > > Peter > > -----------------------------------------> > > Peter Wilkinson > Senior Bioinformatician / Programmer-Analyst > National Immune Monitoring Laboratory > tel: (514)-343-7876 > > ______________________________________________ > R-help at 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 >