Re: [R] Successive subsets from a vector?

From: Prof Brian Ripley <>
Date: Wed 23 Aug 2006 - 01:16:41 EST

On Tue, 22 Aug 2006, hadley wickham wrote:

> > The loop method took 195 secs. Just assigning to an answer of the correct
> > length reduced this to 5 secs. e.g. use
> >
> > ADDRESSES <- character(length(VECTOR)-4)
> >
> > Moral: don't grow vectors repeatedly.
> Other languages (eg. Java) grow the size of the vector independently
> of the number of observations in it (I think Java doubles the size
> whenever the vector is filled), thus changing O(n) behaviour to O(log
> n). I've always wondered why R doesn't do this.

At one point at least that was too expensive on memory/address space (and it may still be for 32-bit OSes). There is even a 'truelength' field in the vector header to allow for such a strategy, and the strategy is used in scan() and elsewhere.

In my experience it is relatively rare not to know the vector length in advance in R code.

Brian D. Ripley,        
Professor of Applied Statistics,
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________ mailing list
PLEASE do read the posting guide
and provide commented, minimal, self-contained, reproducible code.
Received on Wed Aug 23 02:35:14 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 23 Aug 2006 - 04:23:02 EST.

Mailing list information is available at Please read the posting guide before posting to the list.