vectors into a matrix
This is one of those times where do.call() comes in very handy: first
convert the vector names into a list using lapply and get() and then call
do.call with cbind on the list:
do.call(cbind,lapply(paste("VD",1:12,sep=""),get))
This also illustrates the virtue of putting such vectors in a list to begin
with, as then the lapply(paste...) operation can be dispensed with, making
things much more efficient.
Bert Gunter
Genentech Nonclinical Biostatistics
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of Henrique Dallazuanna
Sent: Monday, January 04, 2010 11:00 AM
To: Victor Kyros
Cc: r-help at r-project.org
Subject: Re: [R] vectors into a matrix
Try this:
cbind(ID = as.numeric(gsub("VD", "", ls(patt = "VD\\d+"))) ,
t(sapply(ls(patt = "VD\\d+"), get)))
On Mon, Jan 4, 2010 at 3:32 PM, Victor Kyros <varenda44 at gmail.com> wrote:
Dear all, I'm writing code to do automatically several calculations and gather the results in a matrix. I have the following vectors and I just want to make a matrix with them: VD1 VD2 VD3 ... VD12 *In this case, this series of vectors stops there, in 12, but it could be a higher number or smaller. (for example: VD1, VD2 and VD3; only) (or for example: VD1, VD2, ... till VD78) All vectors have the same length, 9. And vectors should be entered by row. Besides, I would like to add a column with the number of the vector. Something like this: 1 ? ? 44 ? ?23 ? ?0.3 ? ?... 2 ? ? 51 ? ?27 ? ?0.8 ? ?... 3 ? ? 40 ? ?24 ? ?0.6 ? ?... 4 ? ? 47 ? ?18 ? ?0.2 ? ?... 5 ? ? 52 ? ?30 ? ?1.2 ? ?... ... ? ... ? ... ? ... ? ?... 12 ? ?39 ? ?22 ? ?0.5 ? ?... I hope I've explained myself good enough. Thanks in advance for taking the time to help me. Vilen ? ? ? ?[[alternative HTML version deleted]]
______________________________________________ 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 ______________________________________________ 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.