[R] assignment by value or reference

Matt Shotwell matt at biostatmatt.com
Tue Mar 8 16:28:32 CET 2011


On 03/08/2011 07:20 AM, Xiaobo Gu wrote:
> On Wed, Sep 15, 2010 at 5:05 PM, Uwe Ligges
> <ligges at statistik.tu-dortmund.de>  wrote:
>> See the R Language Definition manual. Since R knows about lazy
evaluation,
>> it is sometimes neither by reference nor by value.
>> If you want to think binary, then "by value" fits better than "by
>> reference".
> Hi,
> Can we think it's eventually by value?

Not always (see in-line below).

>
> For simple functions such as:
> is(df[[1]], "logical")
> used to test wheather the first column of data frame df is of type
> logical, will a new vector be created and used inside the is function?

No, df[[1]] isn't copied in this case. However, if you subset an atomic
vector (subset+assignment is different!), there is copying. For example:

 > df <- data.frame(x=c(FALSE,TRUE))
 > tracemem(df[[1]])
[1] "<0x217afa8>"
 > is(df[[1]],    "logical")
[1] TRUE
 > is(df[[1]][],  "logical")
tracemem[0x217afa8 -> 0xf9d198]: ...cut...
[1] TRUE
 > is(df[[1]][1], "logical")
[1] TRUE

Note that tracemem doesn't catch the copying that occurs during 
evaluation of the last expression. As a strategy, R avoids copying when 
it's clearly not necessary from the perspective of the R interpreter. 
There are some notable cases where copying is obviously not necessary 
from the user perspective (e.g. contiguous subsetting), but avoiding a 
copy in these cases might be difficult to implement in R's 
parser/evaluator framework. Here's another simple exception:

 > x <- 1
 > tracemem(x)
[1] "<0x18984b8>"
 > x <- x + 1
tracemem[0x18984b8 -> 0x207e568]: ...cut...

>
> Another example,
>
> dbWriteTable(con, "tablename", df) will write the content of data
> frame df into a database table, will a new data frame object created
> and used inside the dbWriteTable function?

No, but if dbWriteTable modifies its local variable that was assigned
df, then df may be copied.

>
> Thanks.
>
>
>>
>> Uwe Ligges
>>
>>
>>
>> On 05.09.2010 17:19, Xiaobo Gu wrote:
>>>
>>> Hi Team,
>>>
>>>           Can you please tell me the rules of assignment in R, by
value or
>>> by reference.
>>>
>>>>  From my about 3 months of experience of part time job of R, it
seems most
>>>> times it is by value, especially in function parameter and return
values
>>>> assignment; and it is by reference when referencing container
sub-objects of

This is a function call convention (i.e. passing by value), as 
distinguished from an assignment convention (I'm not certain they're
equivalent in R). In general R functions pass by value. There are
exceptions here also, notably R environments. For 
example:

 > f <- function(e) assign("a", 1, e)
 > e <- new.env()
 > f(e)
 > objects(e)
[1] "a"

Under strict pass-by-value convention, e would remain unchanged. In 
general, assignments are by value. However, R environments are an 
exception; assignment is by reference:

 > r <- e
 > objects(r)
[1] "a"
 > assign("b", 2, r)
 > objects(r)
[1] "a" "b"
 > objects(e)
[1] "a" "b"

In this sense, the calling/assignment convention is a property of the 
objects being passed/assigned. I think that is consistent with Uwe's 
comment above.

Best,
Matt
R version 2.12.1 (2010-12-16)
Platform: x86_64-pc-linux-gnu (64-bit)

>>>> container objects, such as elements of List objects and row/column
objects
>>>> of DataFrame objectes; but it is by value when referencing the
smallest unit
>>>> of element of a container object, such as cell of data frame
objects.
>>>
>>>
>>>
>>>
>>>
>>> Xiaobo.Gu
>>>
>>>
>>>
>>>
>>>         [[alternative HTML version deleted]]
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide
>>> http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list