"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. --
Hi What is your intention? basically one output column can be made by cumsum(bd) Then you can shuffle bd by let say bd <- sample(bd, 64) and repeat cumsum for new bd. bd<-scan("blap.txt") output<-matrix(0,64,10) for (i in 1:10) { bd<-sample(bd, 64) cs<-cumsum(bd) output[,i]<-cs } If you insist you can reorder resulting output. Regards Petr> > "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 guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
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.