[R] Automate concatenation?

Peter Dalgaard p.dalgaard at biostat.ku.dk
Tue May 30 14:43:34 CEST 2006


Robert Lundqvist <Robert.Lundqvist at ltu.se> writes:

> I have this typical problem of joining a number of vectors with similar
> names - a1, a2,..., a10 - which should be concatenated into one. Using
> c(a1,a2,a3,a4,a5,a6,a,a8,a9,a10) naturally works, but I would like to do
> it with less manual input. 

And less error-prone (where did the "7" go?)...

> My attempts to use paste() gives a vector of
> the vector names, see below. The question is how to do the the
> concatenation? Any suggestions?
> 
> paste("a",1:10,sep="")

I think this should work:

unlist(lapply( paste("a",1:10,sep=""), get))

(and of course, the usual sermon applies: You're likely better off
using a list of vectors rather than vectors with similar names.)

-- 
   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)                  FAX: (+45) 35327907



More information about the R-help mailing list