Re: [R] Avoiding a memory copy by [[

From: Henrik Bengtsson <hb_at_stat.berkeley.edu>
Date: Wed 24 May 2006 - 02:29:21 EST

On 5/23/06, Matthew Dowle <mdowle@concordiafunds.com> wrote:
>
> Hi,
>
> n = 10000000
> L = list(a=integer(n), b=integer(n))
>
> L[[2]][1:10] gives me the first 10 items of the 2nd vector in the list L.
> It works fine. However it appears to copy the entire L[[2]] vector in
> memory first, before subsetting it. It seems reasonable that "[[" can't
> know that all that is to be done is to do [1:10] on the result and therefore
> a copy in memory of the entire vector L[[2]] is not required. Only a new
> vector length 10 need be created. I see why [[ needs to make a copy in
> general.
>
> L[[c(2,1)]] gives me the 1st item of the 2nd vector in the list L. It
> works fine, and does not appear to copy L[[2]] in memory first. Its much
> faster as n grows large.
>
> But I need more than 1 element of the vector .... L[[c(2,1:10)]] fails
> with "Error: recursive indexing failed at level 2"
>
> Is there a way I can obtain the first 10 items of L[[2]] without a memory
> copy of L[[2]] ?

I think environments will help you out here:

n < 10000000
env <- new.env()

env$a <- integer(n)
env$b <- integer(n)
env$a[1:10]

/Henrik

> Thanks!
> Matthew
>
> R 2.1.1
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>
>



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Wed May 24 02:58:40 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Wed 24 May 2006 - 04:10:17 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.