[R] deep copy?
Duncan Murdoch
murdoch.duncan at gmail.com
Sat Apr 14 15:51:19 CEST 2012
On 12-04-13 8:32 PM, Whit Armstrong wrote:
> Is putting a variable into a list a deep copy (and is tracemem the
> correct way to confirm)?
Yes, it is a deep copy, but done in a lazy way so tracemem won't show
it. The copying won't be done until necessary, i.e. you modify it.
>
> warmstrong at krypton:~/dvl/R.packages$ R
>> x<- rnorm(1000)
>> tracemem(x)
> [1] "<0x3214c90>"
>> x.list<- list(x.in.list=x)
> tracemem[0x3214c90 -> 0x2af0a20]:
>>
>
> Is it possible to put a variable into a list without causing a deep
> copy (i.e. if you _really_ want the objects to share the same
> underlying memory)?
At R level you can simulate it by storing things in environments: they
aren't copied when you do assignments. So
x <- list(a=1, b=new.env())
y <- x
will cause x$a and y$a to be different objects, but x$b and y$b will be
the same thing.
It may be possible to do it with other types in C code, but it will
likely cause corruption when the garbage collector cleans up one copy.
At C level you can do what you like with memory that isn't being managed
by R.
Duncan Murdoch
More information about the R-help
mailing list