[R] using <<- with a changing variable name (substitute?)

Tamas K Papp tpapp at Princeton.EDU
Mon Aug 8 17:22:20 CEST 2005


On Mon, Aug 08, 2005 at 04:07:10PM +0100, Prof Brian Ripley wrote:
> On Mon, 8 Aug 2005, Tamas K Papp wrote:
>
> > m <- function(b) {sapply(b, function(bb) {
> >   if (bb < 0) {bb} else {if (bb>d) {bb-d} else 0}
> > })}
> 
> Why use sapply?
> 
> r[] <- ifelse(r >= d, r-d, ifelse(r >= 0, 0, r))
> 
> is one more efficient way.

Thank you very much, this speeded up things considerably.  I need this
operation to ignore the first column of the matrix, is there a more
efficient way than

r[,-1] <- ifelse(r[,-1] >= d, r[,-1]-d, ifelse(r[,-1] >= 0, 0, r[,-1]))

?

Tamas




More information about the R-help mailing list