Hey guys, I'm trying to write a loop that will repeat an action for a stipulated number of times. I have written some code but i think i'm missing something. for (x in 5) { repeat{ x = runif(1:4, min = 0, max = 1) print(x) if (x== var_1[5]){ print("done") } print(x)} } The goal is to generate the random number 5 times. Please help.... [[alternative HTML version deleted]]
Is there anything wrong with just doing this? x <- runif(5, min = 0, max = 1) Also note that you use x to be at last 2 things: in for (x in 5) { you set it to 5, and then in the loop you x = runif(1:4, min = 0, max = 1) you make it a vector of length 4. You also fail to use break to stop the repeat (something I never knew existed in R!). Bob On Tue, 6 Aug 2019 at 10:54, Tolulope Adeagbo <tolulopeadeagbo at gmail.com> wrote:> > Hey guys, > > I'm trying to write a loop that will repeat an action for a stipulated > number of times. I have written some code but i think i'm missing something. > > for (x in 5) { > > repeat{ > > x = runif(1:4, min = 0, max = 1) > > > print(x) > > > if (x== var_1[5]){ > > print("done") > } > > print(x)} > } > > The goal is to generate the random number 5 times. > > Please help.... > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.-- Bob O'Hara Institutt for matematiske fag NTNU 7491 Trondheim Norway Mobile: +47 915 54 416 Journal of Negative Results - EEB: www.jnr-eeb.org
Hi Tolulope, The "in" operator steps through each element of the vector on the right. You only have one element. Therefore you probably want: for(x in 1:5) ... Jim Jim On Tue, Aug 6, 2019 at 6:54 PM Tolulope Adeagbo <tolulopeadeagbo at gmail.com> wrote:> > Hey guys, > > I'm trying to write a loop that will repeat an action for a stipulated > number of times. I have written some code but i think i'm missing something. > > for (x in 5) { > > repeat{ > > x = runif(1:4, min = 0, max = 1) > > > print(x) > > > if (x== var_1[5]){ > > print("done") > } > > print(x)} > } > > The goal is to generate the random number 5 times. > > Please help.... > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.
Thanks guys, I've tried all you're suggesting, both for (x in 1:5) and break, but I cant seem to ascertain when the loop has generated a vector of 4 random numbers 5 times. On Tue, 6 Aug 2019, 10:09 Jim Lemon, <drjimlemon at gmail.com> wrote:> Hi Tolulope, > The "in" operator steps through each element of the vector on the > right. You only have one element. Therefore you probably want: > > for(x in 1:5) > ... > > Jim > > Jim > > On Tue, Aug 6, 2019 at 6:54 PM Tolulope Adeagbo > <tolulopeadeagbo at gmail.com> wrote: > > > > Hey guys, > > > > I'm trying to write a loop that will repeat an action for a stipulated > > number of times. I have written some code but i think i'm missing > something. > > > > for (x in 5) { > > > > repeat{ > > > > x = runif(1:4, min = 0, max = 1) > > > > > > print(x) > > > > > > if (x== var_1[5]){ > > > > print("done") > > } > > > > print(x)} > > } > > > > The goal is to generate the random number 5 times. > > > > Please help.... > > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > > 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]]