s-news
[Top] [All Lists]

vectorizing

To: S-news <s-news@lists.biostat.wustl.edu>
Subject: vectorizing
From: Ita Cirovic <zag_cirovic@yahoo.com>
Date: Fri, 27 Jul 2007 01:45:31 -0700 (PDT)
Domainkey-signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=X-YMail-OSG:Received:X-Mailer:Date:From:Subject:To:MIME-Version:Content-Type:Message-ID; b=i7Typ4XI9MNTItXGTOsYA9Cmm7HsQ8c6kc0eoPIq+ZukOXbZaR2l5fcFtFcHE8nHWrrZXy4qiTpFukc1mP07yUL8IO2OeUpjexHVnh98LeyhC2tCPDAatzdjyK0ynJMG4v9LUiNqoMncqbzp4WrkxXnqEeYZxgOPMLpoy7hXWMw=;
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 on Yahoo! TV.
<Prev in Thread] Current Thread [Next in Thread>