Hello everyone, I would like to use the "cbind" function to construct a dataset, combining some variable I defined before. The codes are something like var1 <- ... var2 <- ... var2 <- ... ... data <- cbind(var1,var2,var3...) The problem is I would like some flexibity in the data, i.e. the number of the variables are not fixed. Is there a method of combine the uncertain number of variables defined previously? Could anyone give me some advice?Many thanks. -- View this message in context: http://www.nabble.com/cbind-function-tf4799148.html#a13730001 Sent from the R help mailing list archive at Nabble.com.
Try this: sapply(ls(patt="^var[0-9]"), get) On 13/11/2007, livia <yn19832 at msn.com> wrote:> > Hello everyone, > > I would like to use the "cbind" function to construct a dataset, combining > some variable I defined before. > The codes are something like > > var1 <- ... > var2 <- ... > var2 <- ... > ... > data <- cbind(var1,var2,var3...) > > The problem is I would like some flexibity in the data, i.e. the number of > the variables are not fixed. Is there a method of combine the uncertain > number of variables defined previously? > > Could anyone give me some advice?Many thanks. > -- > View this message in context: http://www.nabble.com/cbind-function-tf4799148.html#a13730001 > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
And here is a second solution, that differs in what happens if the variables have differing lengths:> var1 <- 1:4 > var2 <- 1:3 > sapply(ls(patt="^var[0-9]"), get)$var1 [1] 1 2 3 4 $var2 [1] 1 2 3> do.call("cbind", lapply(ls(patt="^var[0-9]"), get))[,1] [,2] [1,] 1 1 [2,] 2 2 [3,] 3 3 [4,] 4 1 Warning message: In cbind(1:4, 1:3) : number of rows of result is not a multiple of vector length (arg 2) Best regards Jens Oehlschl?gel --