

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]]
______________________________________________
[hidden email] mailing list  To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


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 < [hidden email]> 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]]
>
> ______________________________________________
> [hidden email] mailing list  To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/rhelp> PLEASE do read the posting guide http://www.Rproject.org/postingguide.html> and provide commented, minimal, selfcontained, reproducible code.

Bob O'Hara
Institutt for matematiske fag
NTNU
7491 Trondheim
Norway
Mobile: +47 915 54 416
Journal of Negative Results  EEB: www.jnreeb.org
______________________________________________
[hidden email] mailing list  To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


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
< [hidden email]> 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]]
>
> ______________________________________________
> [hidden email] mailing list  To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/rhelp> PLEASE do read the posting guide http://www.Rproject.org/postingguide.html> and provide commented, minimal, selfcontained, reproducible code.
______________________________________________
[hidden email] mailing list  To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, 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, < [hidden email]> 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
> < [hidden email]> 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]]
> >
> > ______________________________________________
> > [hidden email] mailing list  To UNSUBSCRIBE and more, see
> > https://stat.ethz.ch/mailman/listinfo/rhelp> > PLEASE do read the posting guide
> http://www.Rproject.org/postingguide.html> > and provide commented, minimal, selfcontained, reproducible code.
>
[[alternative HTML version deleted]]
______________________________________________
[hidden email] mailing list  To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


For a start, try this:
for(i in 1:5) {
x < runif(4,0,1)
}
Which will do what you want, but will overwrite x each time (so isn't
very good). Better (if you want to use the random numbers outside the
loop) is this:
x < matrix(NA, nrow=5, ncol=4)
for(i in 1:5) {
x[i,] < runif(4,0,1)
}
But better than this is not to use a loop at all, but to use R's vectorisation:
x < matrix(runif(20,0,1), nrow=5, ncol=4)
i.e. generate a vector of random numbers (the runif()), and then put
that into a matrix (the matrix()).
Oh, and you could also do this:
replicate(5, runif(4, 0,1))
which is slightly odd here, but if you want to use the random numbers
to do something, you can do all of it in a function, e.g.
CalcMean < function(n=4) {
x < runif(n, 0, 1)
mean(x)
}
replicate(5, CalcMean(n=4))
Using a function makes code writing a lot easier, as you can write and
debug the function on its own, and then use replicate() to run the
loop (there are also functions like vapply() and apply() if you need
to pass different arguments into the function for different
iterations).
Bob
On Tue, 6 Aug 2019 at 11:28, Tolulope Adeagbo < [hidden email]> wrote:
>
> 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, < [hidden email]> 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
> > < [hidden email]> 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]]
> > >
> > > ______________________________________________
> > > [hidden email] mailing list  To UNSUBSCRIBE and more, see
> > > https://stat.ethz.ch/mailman/listinfo/rhelp> > > PLEASE do read the posting guide
> > http://www.Rproject.org/postingguide.html> > > and provide commented, minimal, selfcontained, reproducible code.
> >
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list  To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/rhelp> PLEASE do read the posting guide http://www.Rproject.org/postingguide.html> and provide commented, minimal, selfcontained, reproducible code.

Bob O'Hara
Institutt for matematiske fag
NTNU
7491 Trondheim
Norway
Mobile: +47 915 54 416
Journal of Negative Results  EEB: www.jnreeb.org
______________________________________________
[hidden email] mailing list  To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


Wow...Great one BOB...Gracias, Merci.
On Tue, 6 Aug 2019, 10:46 Bob O'Hara, < [hidden email]> wrote:
> For a start, try this:
>
> for(i in 1:5) {
> x < runif(4,0,1)
> }
>
> Which will do what you want, but will overwrite x each time (so isn't
> very good). Better (if you want to use the random numbers outside the
> loop) is this:
>
> x < matrix(NA, nrow=5, ncol=4)
> for(i in 1:5) {
> x[i,] < runif(4,0,1)
> }
>
> But better than this is not to use a loop at all, but to use R's
> vectorisation:
>
> x < matrix(runif(20,0,1), nrow=5, ncol=4)
>
> i.e. generate a vector of random numbers (the runif()), and then put
> that into a matrix (the matrix()).
>
> Oh, and you could also do this:
> replicate(5, runif(4, 0,1))
> which is slightly odd here, but if you want to use the random numbers
> to do something, you can do all of it in a function, e.g.
>
> CalcMean < function(n=4) {
> x < runif(n, 0, 1)
> mean(x)
> }
> replicate(5, CalcMean(n=4))
>
> Using a function makes code writing a lot easier, as you can write and
> debug the function on its own, and then use replicate() to run the
> loop (there are also functions like vapply() and apply() if you need
> to pass different arguments into the function for different
> iterations).
>
> Bob
>
>
>
>
> On Tue, 6 Aug 2019 at 11:28, Tolulope Adeagbo < [hidden email]>
> wrote:
> >
> > 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, < [hidden email]> 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
> > > < [hidden email]> 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]]
> > > >
> > > > ______________________________________________
> > > > [hidden email] mailing list  To UNSUBSCRIBE and more, see
> > > > https://stat.ethz.ch/mailman/listinfo/rhelp> > > > PLEASE do read the posting guide
> > > http://www.Rproject.org/postingguide.html> > > > and provide commented, minimal, selfcontained, reproducible code.
> > >
> >
> > [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > [hidden email] mailing list  To UNSUBSCRIBE and more, see
> > https://stat.ethz.ch/mailman/listinfo/rhelp> > PLEASE do read the posting guide
> http://www.Rproject.org/postingguide.html> > and provide commented, minimal, selfcontained, reproducible code.
>
>
>
> 
> Bob O'Hara
> Institutt for matematiske fag
> NTNU
> 7491 Trondheim
> Norway
>
> Mobile: +47 915 54 416
> Journal of Negative Results  EEB: www.jnreeb.org
>
[[alternative HTML version deleted]]
______________________________________________
[hidden email] mailing list  To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.

