s-news
[Top] [All Lists]

Re: vectorizing

To: Patrick Burns <pburns@pburns.seanet.com>
Subject: Re: vectorizing
From: Ita Cirovic <zag_cirovic@yahoo.com>
Date: Fri, 27 Jul 2007 04:59:13 -0700 (PDT)
Cc: S-news <s-news@lists.biostat.wustl.edu>
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:Cc:MIME-Version:Content-Type:Message-ID; b=167kcriosnT38Dq+TXIjw5GnvFWjkreO1Ye1RX7n3NgtVC8OumrFznsjs7nQWwJpiJNlNczcNMKg267ZqdkXRpClSDk/LYbuBR1jGeijtnzhHwg3dmU5SAZ1+KuKYoZa9/RPM7VUG660U8g6cusfDE6ixzzdHzUWGgINmARdd4w=;
if using 'pmin' and 'pmax' shouldn't the args. x and y for the two functions be of the same length where I am trying to use max and mix for the following

x = 11000 x 5
up = 5 x 1
lb = 5 x 1

so I am comparing 11000 rows with only one value of up and lb.

Ita

------------------------------


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.

--------------------------------------------------------------------
This message was distributed by s-news@lists.biostat.wustl.edu.  To
unsubscribe send e-mail to s-news-request@lists.biostat.wustl.edu with
the BODY of the message:  unsubscribe s-news



Park yourself in front of a world of choices in alternative vehicles.
Visit the Yahoo! Auto Green Center.
<Prev in Thread] Current Thread [Next in Thread>