[R] storing output of a loop in a matrix

RH Gibson Rachel.Gibson at bristol.ac.uk
Wed May 23 10:23:04 CEST 2012


"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.


--



More information about the R-help mailing list