[R] Adding values to the end of a vector?

Peter Dalgaard p.dalgaard at biostat.ku.dk
Tue Jan 4 16:00:08 CET 2005


"Liaw, Andy" <andy_liaw at merck.com> writes:

> Is this what you're looking for?
> 
> > x <- numeric(0)
> > for (i in 1:5) x <- append(x, i)
> > x
> [1] 1 2 3 4 5
> 
> Andy

Also notice that in R many things are vectorized, so you may prefer

> append(x,1:5)
[1] 1 2 3 4 5

Extending a vector is done by allocating a longer vector and copying
the original. You really don't want to do that for every element if
you at all can avoid it. So vectorize, or at least preallocate the
extra storage, if you can.

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907




More information about the R-help mailing list