[R] reference to objects

Thomas Lumley tlumley at u.washington.edu
Fri Jan 16 17:09:57 CET 2004


On Fri, 16 Jan 2004, Giampiero Salvi wrote (in reply to some unidentified
helper):

> Thanks for your answer,
>
> > Yes, that will create two copies.  What is it you want to do with the data?
> > Do you want the capability of both of them changing the data?  What type of
> > processing are you going to do?
>
> The data should be read only (and all the objects share the same data values).

If the data are read only it is likely that R will *not* create two
copies, eg try
  a<-list(x=rnorm(1e6))
  gc()
  b<-a$x
  d<-a$x
  gc()
  b<-b+1
  d<-d+1
  gc()

>
> > One way is to store the 'name' of the object in the location and then use
> > 'get/assign' to reference the data:
> >
> > obj1$dist <- 'dist'
> > obj2$dist <- 'dist'
> >
> > my.sum <- sum(get(obj1$data))
> > assign(obj1$data, new.values)
>

This won't work any better.  get(obj1$data) will get the object, and
there's no reason to expect that it's less likely to get a copy than
simple assignment is.

	-thomas




More information about the R-help mailing list