[R] Deep copy in R

Thomas Lumley tlumley at u.washington.edu
Mon Apr 5 23:17:36 CEST 2004


On Mon, 5 Apr 2004, Shin, Daehyok wrote:

> I am handling spatial data of huge volumes,
> so sensitive to the silent duplication of data in script programs.
> In the following R program, exactly when is the vector data deeply copied?
> Thanks in advance.
>
> 1 v <- 1:10000
> 2 z <- f(v)
>
> --------- function f ----------
> 3 f <- function(x) {
> 4   y = x
> 5   y[10] = 1
> 6   xf = date.frame(x=x)
> 7   xf$x[10] = 1
> 8   return(y)
> }
>

There will be a copy at line 5 and at line 6 or line 7 or both.

Copying occurs when there are (or could be) two references to an object
and one of them is modified.

Passing v to the function f creates a second reference to it (the local
variable x), and then y is a third reference. Modifying y forces a copy so
that x and v don't get modified

The data.frame() call need not copy, but I think it actually does. If it
does, modifying xf$x need not copy.

Returning y does not copy, and xf is discarded for garbage collection when
the function exits.


	-thomas




More information about the R-help mailing list