You need to be cognizant of two things:
1) the recycling rule in which vectorized operations
will (typically) extend short vectors to be the length
of the longest vector.
2) matrices are just vectors that have one or two
special attributes. They are stored down the columns.
That is, the vector is the first column, then the second
column, etc.
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:
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>
|