[R] adding columns to a table after a loop
Uwe Ligges
ligges at statistik.uni-dortmund.de
Sat Aug 12 16:57:16 CEST 2006
Albert Picado wrote:
> Dear list,
>
> I am trying to find the way to add columns to a table
> (or a matrix) after a loop. The result of this loop is
> an array and I would like to get all the results in a
> single table once the loop is finish. I have
> reproduced a simplified example below:
>
>> a<- 1
>> b <- matrix()
>> while (a <= 4) {
> + b<-rnorm(10)
> + a<- a +1
> + }
Staying in your example:
a <- 1
b <- matrix(nrow=10, ncol=4)
while(a <= 4){
b[,i] <- rnorm(10)
a <- a + 1
}
Uwe Ligges
> # I have tried several methods but without succes so
> far.
>
>> result <- as.data.frame(cbind(b))
>> result
> b
> 1 -0.03250661
> 2 -0.59823770
> 3 1.58120471
> 4 0.41086546
> 5 0.78959090
> 6 1.23587125
> 7 0.83427190
> 8 1.09035581
> 9 0.11331056
> 10 0.25267231
>
> the result above is not what I am looking for because
> it shows only the results from the last operation in
> the loop, I would like to obtain something like the
> following.
>
>> result
> b1 b2 b3 b4
> 1 -0.08420826 -0.0637943 -0.83246201 -1.0902384
> 2 0.40623009 -2.7940096 1.37664973 1.6023967
> 3 -1.13850505 1.1660669 -0.95962296 -0.7325098
> 4 -1.06183391 1.1063677 1.67948677 -1.9875475
> 5 -0.64431067 -0.4843952 -1.30742858 0.5064134
> 6 0.56729468 1.0860484 0.07651954 0.4380108
> 7 0.95036177 -0.5328609 1.28954934 -0.2775614
> 8 -0.17848223 0.5340379 -0.22613700 1.0179886
> 9 2.93145454 -1.8639607 -0.25478610 1.6619754
> 10 1.51942415 -0.2051423 0.09144450 -1.6329481
>
> I hope someone can give me a hand
>
> best regards
>
> a
>
>
>
>
>
> p5.vert.ukl.yahoo.com uncompressed/chunked Sat Aug 12 13:14:00 GMT 2006
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
More information about the R-help
mailing list