Skip to content
Prev 303334 / 398506 Next

pass by reference

You can write the replacement-function like [the untested[
   `col2<-` <- function(x, ..., value) {
       x$col2[x$col1 < 2] <- value
       x
    }
so you can do modifications with the syntax
   col2(data) <- "L"
If you also write the matching extractor
   col2 <- function(x) x$col2[x$col1 < 2]
then you can nest replacements with
   col2(data)[2] <- "L"

I like this syntax because it makes clear what is being modified
(things to the left of the assignment operator).  It may
or may not save memory or time, but as R develops 
replacement functions may get more efficient.

By the way, the syntax
    f <- function(data) {
        data <<- modify(data)
    }
is terrible because calling f(argument) creates a dataset called 'data'
(not 'argument') in some ancestral environment of the function (which
environment depends on where a previous dataset called 'data'
may have been defined). 

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com