[R] Using vectorization instead of for loop for performing a calculation efficiently
Peter Wilkinson
pwilkinson at videotron.ca
Sun Apr 2 01:52:55 CEST 2006
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 dont 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
More information about the R-help
mailing list