[R] Successive subsets from a vector?
Prof Brian Ripley
ripley at stats.ox.ac.uk
Tue Aug 22 17:16:41 CEST 2006
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, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UK Fax: +44 1865 272595
More information about the R-help
mailing list