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]]
On Jan 4, 2010, at 12:32 PM, Victor Kyros 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 >cbind( 1:12, matrix(c(VD1, ..., VD12), nrow=12, byrow=TRUE) )> *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.David Winsemius, MD Heritage Laboratories West Hartford, CT
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
Hello all,
Firstly, thanks a lot for all your efforts,
the cbind function was very useful.
I tried all you told me, but I couldn't make it work in the way I wanted.
I mixed two problems I had, a common mistake.
Sorry if I didn't explain myself good enough.
Here, I post a solution for my problem.
I wanted to avoid the "while" loop but I've finally used it.
Hopefully it is helpfull for someone else.
CODE:
-------------------------------
# This could be my data:
VD1 <- c(12, 34, 45, 7, 67, 45)
VD2 <- c(23, 12, 45, 67, 89, 90)
VD3 <- c(14, 11, 10, 19, 20, 27)
VD4 <- c(16, 22, 23, 29, 27, 28)
# and this is my objective:
# (in this case it is just for 4 vectors)
AIM <- matrix(c(VD1, VD2, VD3, VD4), nrow=4, byrow=TRUE)
print(AIM)
# but I want to use any number of vectors
# VDx when x goes from 1 to n
n <- 4 # for this case.
# A solution:
# build an empty matrix with the desired number of rows (vectors)
# then with a "while" loop fill each row with each vector.
Final.matrix <- matrix(, nrow=n, ncol=6, byrow=TRUE)
print(Final.matrix)
y <- 1
while (y <= n)
{
c <- eval(parse(text=(paste("VD", sep="", y))))
Final.matrix[y,] <- c
y <- y + 1
}
print(Final.matrix)
# If I set "n" as 12 I will get the example I explained at first
# then, by using "cbind()" and "1:n" I add the values of the
first column
# as many of you suggested to me
---------------------------------------
If someone comes up with a way to do this avoiding the loop, I'd be very
interested to get to know the solution.
Kind regards,
Vilen
Forestry Engineer
Seemingly Similar Threads
- sem package and growth curves
- R CMD check . segfault on re-building vignettes
- file.copy(src, src, recursive=TRUE) causes a segfault (Was: Re: R CMD check . segfault on re-building vignettes)
- Winbind idmap partially fails to load attributes with 4.6.7 (Ubuntu 17.10)
- Winbind idmap partially fails to load attributes with 4.6.7 (Ubuntu 17.10)