[R] Re : adding columns to a table after a loop
Uwe Ligges
ligges at statistik.uni-dortmund.de
Sat Aug 12 18:53:03 CEST 2006
Albert Picado wrote:
> Dear Uwe,
>
> Thanks for your answer. I have been trying your suggestion but I haven't been able to solve my problem as I may have simplified in excess my example. The real situation is actually much similar to the following:
>
> a<-1
> b<-1
> i<-1
> pvalue<-matrix()
> while(a<=4) {
> while(b<=10){
> pvalue[i]<-rnorm(1)
> i<-i+1
> b<-b+1
> }
> a<-a+1
> }
I think you will have to read some basic documentation such as "An
Introduction to R" about data structures, indexing, and loops in R
before proceeding ...
Anyway, you can do:
a <- 1
pvalue <- matrix(nrow = 10, ncol = 4)
while(a <= 4) {
b <- 1
while(b <= 10){
pvalue[b, a] <- rnorm(1)
b <- b+1
}
a <- a+1
}
or much shorter
pvalue <- matrix(nrow = 10, ncol = 4)
for(a in 1:4) {
for(b in 1:10){
pvalue[b, a] <- rnorm(1)
}
}
Uwe Ligges
> To get the desired outcome I have tried two things based on your suggestion:
>
> 1)
>
> a<-1
> b<-1
> i<-1
> pvalue<- matrix(ncol=4, nrow=10)
> result<- matrix()
> while(a<=4) {
> while(b<=10){
> pvalue[i]<-rnorm(1)
> i<-i+1
> b<-b+1
> }
> result[,a]<-pvalue
> a<-a+1
> }
>
> here I get an error message:
> Error in "[<-"(`*tmp*`, , a, value = c(1.38661984066808, -0.641565430307799, :
> number of items to replace is not a multiple of replacement length
>
> 2)
>
> a<-1
> b<-1
> i<-1
> pvalue<-matrix(ncol=4, nrow=10)
> while(a<=4) {
> while(b<=10){
> pvalue[i,a]<-rnorm(1)
> i<-i+1
> b<-b+1
> }
> a<-a+1
> }
>
> here what I get is:
>
>> pvalue
> [,1] [,2] [,3] [,4]
> [1,] -2.18379100 NA NA NA
> [2,] -0.90112392 NA NA NA
> [3,] -0.91762163 NA NA NA
> [4,] 0.54922398 NA NA NA
> [5,] 0.95511590 NA NA NA
> [6,] 0.08509518 NA NA NA
> [7,] -0.92576835 NA NA NA
> [8,] 1.62361191 NA NA NA
> [9,] 0.02644486 NA NA NA
> [10,] 0.32406407 NA NA NA
>
>
> I do not know what else to try...
>
> best wishes
>
> a
>
>
> ----- Message d'origine ----
> De : Uwe Ligges <ligges at statistik.uni-dortmund.de>
> À : Albert Picado <albert_picado at yahoo.fr>
> Cc : r-help at stat.math.ethz.ch
> Envoyé le : Samedi, 12 Août 2006, 3h57mn 16s
> Objet : Re: [R] adding columns to a table after a loop
>
>
> 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