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

2006-04-06 Thread Peter Wilkinson
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:[EMAIL PROTECTED] 
Sent: Saturday, April 01, 2006 7:58 PM
To: Peter Wilkinson
Cc: r-help@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(2 * 10), 2)
 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 [EMAIL PROTECTED] 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(nvectorfilter,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@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


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

2006-04-01 Thread Peter Wilkinson
 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(nvectorfilter,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@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


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

2006-04-01 Thread Gabor Grothendieck
Try this:

 set.seed(1)
 mat - matrix(rnorm(2 * 10), 2)
 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 [EMAIL PROTECTED] 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(nvectorfilter,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@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