[R] Deep copy in R

Shin, Daehyok sdhyok at catchlab.org
Tue Apr 6 03:32:38 CEST 2004


Hm. Smarter than I expected.
But, any special reason why the 6th line clones another vector?
To me, just reference copy seems to be enough for the purpose.

6 xf = date.frame(x=x)

Daehyok Shin

> -----Original Message-----
> From: r-help-bounces at stat.math.ethz.ch
> [mailto:r-help-bounces at stat.math.ethz.ch]On Behalf Of Thomas Lumley
> Sent: Monday, April 05, 2004 PM 5:18
> To: Shin, Daehyok
> Cc: R, Help
> Subject: Re: [R] Deep copy in R
>
>
> 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
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html




More information about the R-help mailing list