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 17:25:12 +0100
Cc: S-news <s-news@lists.biostat.wustl.edu>
In-reply-to: <208987.64409.qm@web51409.mail.re2.yahoo.com>
References: <208987.64409.qm@web51409.mail.re2.yahoo.com>
User-agent: Mozilla Thunderbird 1.0.7 (Windows/20050923)
If I understand the problem correctly, you
will be set if you transpose 'x'.

Ita Cirovic wrote:

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. <http://us.rd.yahoo.com/evt=48246/*http://autos.yahoo.com/green_center/;_ylc=X3oDMTE5cDF2bXZzBF9TAzk3MTA3MDc2BHNlYwNtYWlsdGFncwRzbGsDZ3JlZW4tY2VudGVy>


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