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

From: Prof Brian Ripley <ripley_at_stats.ox.ac.uk>
Date: Tue 09 Aug 2005 - 01:07:10 EST

On Mon, 8 Aug 2005, Tamas K Papp wrote:

> I have a matrix r and a scalar d, and I would like to apply the
> following functions to each of its elements:
>
> 1. if r < 0, no change
> 2. if 0 <= r < d, replace element by zero
> 3. if d <= r, replace element by r-d
>
> I wrote a small function for this
>
> 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.

>
> so I can simply say r <- m(r). The problem is that the matrix r is
> huge and only one of them fits in the memory,

If that is literally true, you cannot do this at R level AFAICS. The only way I can see that you can do this with standard semantics is

m(r) <- d

with a replacement function m<-() written using .Call. Using

"m<-" <- function(r, d) ifelse(r >= d, r-d, ifelse(r >= 0, 0, r))

is going to make several copies.

> and I don't need the
> original r, so I would like to do this memory-efficiently. Moreover,
> there are matrices with various names (not only r) so I need a generic
> function (they don't fit in memory at the same time, I load, save and
> rm them).
>
> I tried various combinations of <<-, assign, substitute etc. but could
> not get it working. Could somebody please help me?

-- 
Brian D. Ripley,                  ripley@stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________
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:11:29 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:39:45 EST