Skip to content
Back to formatted view

Raw Message

Message-ID: <20050808142648.GA27795@tpapp.student.princeton.edu>
Date: 2005-08-08T14:26:48Z
From: Tamas K Papp
Subject: using <<- with a changing variable name (substitute?)

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}
  })}
 
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, 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?

Tamas