[R] Function to modify existing data.frame

Jan T. Kim jtk at cmp.uea.ac.uk
Tue Jan 18 18:49:42 CET 2005


On Tue, Jan 18, 2005 at 09:31:33AM -0600, Marc Schwartz wrote:
> On Tue, 2005-01-18 at 10:18 -0500, 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?

> What type of modifications?
> 
> Take a look at:
> 
> ?replace
> ?transform

I know neither the original poster nor Stata, but as I understand the
question, this answer may not be exactly what Peter has been looking
for. As the docs on replace state:

    x is unchanged: remember to assign the result.

So, something like

    x <- replace(x, 4:6, 3:5 * 3)

does not eliminate creation of a copy of x. A function that modifies
its argument would require a call by reference mechanism, which isn't
provided by R.

Some time ago, I looked quite extensively for a call by reference facility
in R, but found that there is none. In the archives of this mailing list
and elsewhere, various alternatives to call by reference approaches are
suggested. As far as I can see, the issues of performance hits incurred
by unnecessary copying of large objects, and of implementing true state
changes in object oriented programming cannot fully be resolved, however.

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