[R] storing output of a loop in a matrix

Rainer Schuermann rainer.schuermann at gmx.net
Wed May 23 10:51:57 CEST 2012


Try

for( i in 1:10 ){
...
}

That should resove your problem 1.!

Rgds,
Rainer



On Wednesday 23 May 2012 09:23:04 RH Gibson wrote:
> "blap.txt" is a numeric vector of length 64.
> 
> I am using the following code:
> 
> 
> bd<-scan("blap.txt")
> output<-matrix(0,64,10)
> s<-sum(bd)
> for (i in 10){
> 
> while (s>0)
> {x1<-sample(bd,(length(bd)-1), replace=F)
> s<-sum(x1)
> bd<-x1
> output[i]<-s
> 
> }
> 
> 
> }
> write.table(output, file="res.txt")
> 
> This code is not doing what I'd like it to do:
> 
> 1. It is not running through the while loop 10 times, just once.
> 2. I can't work out how to get the results into the output matrix. The
> matrix should be 10 columns with the decreasing s values for each of the
> 10 runs through the for loop.
> 
> Sorry for any obvious mistakes. I'm very new to R and teaching myself.
> Where am I going wrong here, please?
> 
> Thank you.
> 
> 
> --
> 
> ______________________________________________
> 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.



More information about the R-help mailing list