[R] Function to modify existing data.frame

Jan T. Kim jtk at cmp.uea.ac.uk
Tue Jan 18 22:20:27 CET 2005


On Tue, Jan 18, 2005 at 09:35:26AM -0800, Thomas Lumley wrote:
> On Tue, 18 Jan 2005, Peter Muhlberger wrote:
> 
> >I'm used to statistical languages, such as Stata, in which it's trivial to
> >pass a list of variables to a function & have that function modify those
> >variables in the existing dataset rather than create copies of the 
> >variables
> >or having to replace the entire dataset to change a few variables.  In R, I
> >suppose I could paste together the right instructions in a function and 
> >then
> >execute it, but is there any more straightforward way of doing this I'm
> >missing?

> There are two ways to get the effect you are looking for. I don't 
> recommend either, though.

This leaves me wondering which way you actually would recommend? After
all, one cannot entirely recommend against the concept of a function
(sensu lato, perhaps) which modifies a few members of a (possibly
very large) object in place, without copying the entire object in the
process.

> 1) Store your variables in an environment, rather than a data frame. 
> Environments are passed by reference.

This is the approach used in the OOP package -- so, while implementing
the data set as an object in this way is probably substantially "more"
than what Peter asked for, wouldn't this be an interesting option to
at least look into?

> 2) The right combinations of eval() and substitute() and lazy evaluation 
> make it possible to write macros in R.  There's an R Newsletter article 
> about this.

Ok, this refers to the "defmacro" technique, if I dereference this call
by reference correctly  ;-)

Best regards, Jan
-- 
 +- Jan T. Kim -------------------------------------------------------+
 |    *NEW*    email: jtk at cmp.uea.ac.uk                               |
 |    *NEW*    WWW:   http://www.cmp.uea.ac.uk/people/jtk             |
 *-----=<  hierarchical systems are for files, not for humans  >=-----*




More information about the R-help mailing list