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.
|