[R] extendable arrays in R

Brad Thompson yak at MIT.EDU
Thu Mar 2 19:40:16 CET 2006


In an R program I am working on, I progressively build up several
vectors of integers.  I never know how long the vectors will eventually
be, so I can't preallocate them with vector().  If I preallocate all of
the vectors to their maximum size, I will run out of memory.  I tried
using c() or append() to build up the vectors, as in the following (tried
on R 2.1.0 on Linux and R 2.2.1 on MacOS 10):

  li <- vector()       # also tried list() and pairlist()
  for (i in 0:40000) {
    li <- c(li, i)     # also tried with i and li swapped
    if (i %% 10000 == 0)
      system('date')
  }

The problem is that based on the times this outputs, it is O(n^2),
matching the straightforward implementation of c() where everything
passed to c() is copied.

I tried extending the vector by assigning to length(li) instead of
using c(), but that also runs in O(n) (so the loop runs in O(n^2)) and
appears to copy the elements.

What I am looking for is an array that can dynamically resized in
amortized constant or log time (like python's list or C++'s
std::vector).  I could build up a data structure inside R (the same way
std::vector is built on top of C arrays), but I was hoping someone
might have some advice on a better way to do this.

Does R have a resizeable array type that I'm missing?  What do others
generally do in this case?

Thank you,

Brad




More information about the R-help mailing list