Peter Muhlberger
2005-Jan-19 17:27 UTC
[R] Function to modify existing data.frame--Improving R Language
Thanks to everyone who replied to my question! A comment regarding Thomas's point below: Java & other object-oriented languages that are function rather than macro based usually provide methods & other capabilities that allow pass by reference in certain cases and these methods would solve the problem I posed regarding how to change values in an existing data.frame from within a function. In Java, for example, objects pass by reference, so I could easily transfer a dataset to a function & have it efficiently changed. In Visual Basic, one could indicate whether a variable was being passed by reference or value. By not allowing any straightforward passing by reference, R strikes me as a lot less flexible & useful than it might be. A basic operation in other stats languages is to update a dataset using a program. This proves very helpful for managing data and setting up analyses. But, this seems to be quite inelegant to do in R. Peter On 1/19/05 6:16 AM, "r-help-request at stat.math.ethz.ch" From: Thomas Lumley <tlumley at u.washington.edu> <r-help-request at stat.math.ethz.ch> wrote:> 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.
Thomas Lumley
2005-Jan-19 18:31 UTC
[R] Function to modify existing data.frame--Improving R Language
On Wed, 19 Jan 2005, Peter Muhlberger wrote:> By not allowing any > straightforward passing by reference, R strikes me as a lot less flexible & > useful than it might be. A basic operation in other stats languages is to > update a dataset using a program. This proves very helpful for managing > data and setting up analyses. But, this seems to be quite inelegant to do > in R.I don't see why mydata <- some.program(mydata) is much less elegant than mydata.someProgram() as a way of updating a data set. It may use more memory, but that wasn't the point at issue. Of course there are advantages to the ability to pass by reference, and disadvantages -- the most obvious disadvantage is that it is not easy to tell which variables are modified by a given piece of code. It probably wouldn't be that hard to produce something that looked like a data frame but was passed by reference, by wrapping it in a environment. -thomas> > Peter > > On 1/19/05 6:16 AM, "r-help-request at stat.math.ethz.ch" From: Thomas Lumley > <tlumley at u.washington.edu> > <r-help-request at stat.math.ethz.ch> wrote: > >> 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. > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >Thomas Lumley Assoc. Professor, Biostatistics tlumley at u.washington.edu University of Washington, Seattle