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

From: Tamas K Papp <tpapp_at_princeton.edu>
Date: Tue 09 Aug 2005 - 01:22:20 EST

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



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Tue Aug 09 01:30:47 2005

This archive was generated by hypermail 2.1.8 : Sun 23 Oct 2005 - 15:09:42 EST