[R] Using vectorization instead of for loop for performing a calculation efficiently

Gabor Grothendieck ggrothendieck at gmail.com
Sun Apr 2 02:58:09 CEST 2006


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
>




More information about the R-help mailing list