s-news
[Top] [All Lists]

Re: vectorizing

To: Ita Cirovic <zag_cirovic@yahoo.com>
Subject: Re: vectorizing
From: Patrick Burns <pburns@pburns.seanet.com>
Date: Fri, 27 Jul 2007 09:58:39 +0100
Cc: S-news <s-news@lists.biostat.wustl.edu>
In-reply-to: <226148.46658.qm@web51407.mail.re2.yahoo.com>
References: <226148.46658.qm@web51407.mail.re2.yahoo.com>
User-agent: Mozilla Thunderbird 1.0.7 (Windows/20050923)
I think you are looking for 'pmin' and 'pmax'.

Patrick Burns
patrick@burns-stat.com
+44 (0)20 8525 0696
http://www.burns-stat.com
(home of S Poetry and "A Guide for the Unwilling S User")

Ita Cirovic wrote:

Hi,

I have the following code and I am trying to use apply functions to get rid of the for loops, so far I have the following. Any suggestions about the remaining for loop as this takes a bit long given that x has around 15000 observations. Thanks.

# original
for (i in 1:length(x)){      # x is a matrix
    for (j in 1:length(x[,1]){
       wx[j] <- max(min(x[j,],up[i]),lb[i])
       wr[j,i] <- wx[j]
    }
}


# replaced one for loop with apply

wr <- apply(x,2,function(x){
for (i in 1:length(x)) max(min(x,up[i]),lb[i])
                       })

where up and lb are vectors of length the same as x matrix.

------------------------------------------------------------------------
Ready for the edge of your seat? Check out tonight's top picks <http://us.rd.yahoo.com/evt=48220/*http://tv.yahoo.com/> on Yahoo! TV.


<Prev in Thread] Current Thread [Next in Thread>