[R] Function to modify existing data.frame--Improving R Language
Thomas Lumley
tlumley at u.washington.edu
Wed Jan 19 19:31:13 CET 2005
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
More information about the R-help
mailing list