Hi > -----Original Message----- > From: r-help-boun...@r-project.org [mailto:r-help-bounces@r- > project.org] On Behalf Of bett kimutai > Sent: Wednesday, June 26, 2013 3:17 AM > To: Law, Jason; r-help@r-project.org > Subject: Re: [R] Loops > > Thanks for you response. The issue is that I need to run a thousand > simulations for each pipe (20000) and then get an average of the values > that are below the condition I have set. When I ran the code, I only > get just a thousand values meaning it gives me values for a single
How do you get result with syntax error code? Anyway did you give a chance to help provided by Jason? If I understand your code, you have three "k" values based on them you compute three "l" values with a differnt constant each time. Based on this you can get > k<-c(1.15, .504, .43) > (7.16-0.44+0.12-0.016) [1] 6.824 > (8.01-1.5+0.35+0.45) [1] 7.31 > 9.55-2.45+0.40+0.65 [1] 8.15 > m<-c(6.824,7.31,8.15) > l=exp((1/k)*(m)) > l [1] 3.776293e+02 1.990643e+06 1.703709e+08 then you want to get z vector in 3 steps z <- (log(1/p)*l[1])^k[1] z[z<=684] <- (log(1/p)*l[2])^k[2] z[z<=684] <- (log(1/p)*l[3])^k[3] you maybe want to subset also p and you need top repeat this 1000 times probably in loop to populate resulting matrix. Anyyway some example of data and working code would be helpful. Regards Petr > pipe. How can I rectify my loop to run for all the pipes and then > create a table to hold these values. Sorry for the syntax errors, > I guess I will get them with time. > > > thanks again.. > > ________________________________ > From: "Law, Jason" <jason....@portlandoregon.gov> > > ect.org> > Sent: Tuesday, June 25, 2013 3:32 PM > Subject: RE: [R] Loops > > > Not sure what you're trying to do, but it looks like most of what > you're attempting to do in the code can be done just using vectors > rather than loops, at least the inner loop. For example: > > k <- 1.15 > l <- exp((1 / k) * (7.16 - 0.44 + 0.12 - 0.016)) > z <- (log(1 / p) * l)^k > > See ifelse for how to do the if tests on a vector. In addition, much > of the code in your loops doesn't vary with the loop indices and can be > moved outside your loop (e.g., setting k = 1.15). If you really > want/need to use loops, you'll have to initialize the vectors/matrices > within your loop with some value: > > z <- numeric(1000) > > Finally, you have some plain syntax errors: > > p[i]=[i+1]. > > That's not valid R code; '[' is the extraction operator, see > help('['). I'm not sure what you're trying to do there. Perhaps: > > p[i] <- p[i + 1] > > HTH, > > Jason Law > > > > -----Original Message----- > From: r-help-boun...@r-project.org [mailto:r-help-bounces@r- > project.org] On Behalf Of bett kimutai > Sent: Tuesday, June 25, 2013 1:32 PM > To: r-help@r-project.org > Subject: [R] Loops > > Dear All, > > > I have spent most of my time trying to figure out how to simulate the > number of breaks in a pipe using Monte Carlo simulation. > i have 20,000 individual pipes that i have to run, and for each pipe i > have to run 1000 times while checking some conditions and therefore, i > have to use a nested loop. > what i would like to have as a final result is a matrix table with with > all the individual pipe elements and the simulated runs here is the > loop that i tried to create x=20000 y=matrix(x, z) > p=runif(1000) > for(j in 1:20000) { > for(i in 1:1000) { > k=1.15 > l=exp((1/k)*(7.16-0.44+0.12-0.016)) > > z[i]=(log(1/p[i])*l)^k > > if (z[i] <=684) > { > k1=0.504 > l1=exp((1/k)*(8.01-1.5+0.35+0.45)) > z1[i]=(log(1/p[i])*l1)^k1 > if (z1[i] <=684) > { > k2=0.43 > l2=exp((1/k2)*(9.55-2.45+0.40+0.65)) > z2[i]=(log(1/p[i])*l2)^k2 > p[i]=[i+1] > break() > } > } > } > x[ j ]=[ j+1 ] > } > the last column of the table, in addition to the simulated runs, i > would like to have the summary of the means (for z<=684) of individual > row as this means will give me the number of breaks. > i will really appreciate if anyone who can help me figure out how to go > about this. pardon me, I am new to R and programming. > > > > Thank you in Advance, > > Eliab > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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. > [[alternative HTML version deleted]] > [[alternative HTML version deleted]] ______________________________________________ R-help@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.