[R] Using vectorization instead of for loop for performing a calculation efficiently
Peter Wilkinson
pwilkinson at videotron.ca
Fri Apr 7 05:51:44 CEST 2006
Thanks,
Ok so I feel silly now ... I think I need to get a more thorough text and
work through some examples. 'replace' does what I was trying to do manually.
Thanks for the tip, this really is MUCH faster.
-----Original Message-----
From: Gabor Grothendieck [mailto:ggrothendieck at gmail.com]
Sent: Saturday, April 01, 2006 7:58 PM
To: Peter Wilkinson
Cc: r-help at stat.math.ethz.ch
Subject: Re: [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
>
More information about the R-help
mailing list