[R] Function to modify existing data.frame
Thomas Lumley
tlumley at u.washington.edu
Tue Jan 18 18:35:26 CET 2005
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?
>
Yes and no.
This isn't so much a question of pass-by-reference, as one reply
suggested, but of macros vs functions.
Stata is (largely) a macro language: it does operations on command strings
and then evaluates them. It's not that Stata programs work with
references, it's that all objects (except local macros) are global.
R is based on functions: it evaluates arguments and then operates on
them. When you have functions, with local variables, it then becomes
relevant to ask whether the arguments to the function are just copies or
are references to the real thing. In R they are just copies (from a
language point of view) but are often references from an efficiency point
of view.
There are two ways to get the effect you are looking for. I don't
recommend either, though.
1) Store your variables in an environment, rather than a data frame.
Environments are passed by reference.
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.
-thomas
More information about the R-help
mailing list