Iteration in R

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

Iteration in R

Martin Zonyrah
Hi,
I need help. I am trying to iterate this command  x <- rnorm(100, 1.0, 2.0) one hundred times in R but I don't seem to have a clue.
Can anyone help?
Your help is very much appreciated.

Martin

        [[alternative HTML version deleted]]


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Iteration in R

David Winsemius

On Dec 3, 2011, at 1:10 PM, Martin Zonyrah wrote:

> Hi,
> I need help. I am trying to iterate this command  x <- rnorm(100,  
> 1.0, 2.0) one hundred times in R but I don't seem to have a clue.

?replicate
>

==

David Winsemius, MD
West Hartford, CT

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Iteration in R

Michael Weylandt
In reply to this post by Martin Zonyrah
? replicate

or a for loop

or do all one hundred simulations at once

x <- matrix(rnorm(100^2, 1, 2), 100)

It's going to depend on what you want to do with the numbers.

Michael

On Sat, Dec 3, 2011 at 1:10 PM, Martin Zonyrah <[hidden email]> wrote:

> Hi,
> I need help. I am trying to iterate this command  x <- rnorm(100, 1.0, 2.0) one hundred times in R but I don't seem to have a clue.
> Can anyone help?
> Your help is very much appreciated.
>
> Martin
>
>        [[alternative HTML version deleted]]
>
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Iteration in R

andrija djurovic
In reply to this post by Martin Zonyrah
Hi. One approach is using replicate. See ?replicate:

replicate(3,rnorm(100,1,2))

Andrija

On Sat, Dec 3, 2011 at 7:10 PM, Martin Zonyrah <[hidden email]>wrote:

> Hi,
> I need help. I am trying to iterate this command  x <- rnorm(100, 1.0,
> 2.0) one hundred times in R but I don't seem to have a clue.
> Can anyone help?
> Your help is very much appreciated.
>
> Martin
>
>        [[alternative HTML version deleted]]
>
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Iteration in R

Brad Schneid
In reply to this post by Michael Weylandt
Hi Michael,
How would you do this with lapply to return a list?
I can't seem to get that to work (I haven't used these much and am trying to learn).
Thanks
Brad

Michael Weylandt wrote
? replicate

or a for loop

or do all one hundred simulations at once

x <- matrix(rnorm(100^2, 1, 2), 100)

It's going to depend on what you want to do with the numbers.

Michael

On Sat, Dec 3, 2011 at 1:10 PM, Martin Zonyrah <[hidden email]> wrote:
> Hi,
> I need help. I am trying to iterate this command  x <- rnorm(100, 1.0, 2.0) one hundred times in R but I don't seem to have a clue.
> Can anyone help?
> Your help is very much appreciated.
>
> Martin
>
>        [[alternative HTML version deleted]]
>
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Iteration in R

andrija djurovic
Hi Brad. Maybe something like this:

lapply(rep(1,6), function(x) rnorm(10,0,1))

Andrija

On Sat, Dec 3, 2011 at 8:21 PM, B77S <[hidden email]> wrote:

> Hi Michael,
> How would you do this with lapply to return a list?
> I can't seem to get that to work (I haven't used these much and am trying
> to
> learn).
> Thanks
> Brad
>
>
> Michael Weylandt wrote
> >
> > ? replicate
> >
> > or a for loop
> >
> > or do all one hundred simulations at once
> >
> > x <- matrix(rnorm(100^2, 1, 2), 100)
> >
> > It's going to depend on what you want to do with the numbers.
> >
> > Michael
> >
> > On Sat, Dec 3, 2011 at 1:10 PM, Martin Zonyrah &lt;martin2005z@&gt;
> wrote:
> >> Hi,
> >> I need help. I am trying to iterate this command  x <- rnorm(100, 1.0,
> >> 2.0) one hundred times in R but I don't seem to have a clue.
> >> Can anyone help?
> >> Your help is very much appreciated.
> >>
> >> Martin
> >>
> >>        [[alternative HTML version deleted]]
> >>
> >>
> >> ______________________________________________
> >> R-help@ 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.
> >>
> >
> > ______________________________________________
> > R-help@ 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.
> >
>
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/Iteration-in-R-tp4154433p4154479.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Iteration in R

Brad Schneid
Interesting and thank you; I'm confused as to why this doesn't work with:

lapply(rep(1,6), FUN=rnorm, n=10, mean=1.0, sd=1)


andrija djurovic wrote
Hi Brad. Maybe something like this:

lapply(rep(1,6), function(x) rnorm(10,0,1))

Andrija

On Sat, Dec 3, 2011 at 8:21 PM, B77S <[hidden email]> wrote:

> Hi Michael,
> How would you do this with lapply to return a list?
> I can't seem to get that to work (I haven't used these much and am trying
> to
> learn).
> Thanks
> Brad
>
>
> Michael Weylandt wrote
> >
> > ? replicate
> >
> > or a for loop
> >
> > or do all one hundred simulations at once
> >
> > x <- matrix(rnorm(100^2, 1, 2), 100)
> >
> > It's going to depend on what you want to do with the numbers.
> >
> > Michael
> >
> > On Sat, Dec 3, 2011 at 1:10 PM, Martin Zonyrah <martin2005z@>
> wrote:
> >> Hi,
> >> I need help. I am trying to iterate this command  x <- rnorm(100, 1.0,
> >> 2.0) one hundred times in R but I don't seem to have a clue.
> >> Can anyone help?
> >> Your help is very much appreciated.
> >>
> >> Martin
> >>
> >>        [[alternative HTML version deleted]]
> >>
> >>
> >> ______________________________________________
> >> R-help@ 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.
> >>
> >
> > ______________________________________________
> > R-help@ 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.
> >
>
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/Iteration-in-R-tp4154433p4154479.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Iteration in R

Michael Weylandt
In reply to this post by andrija djurovic
And with replicate:

replicate(100, rnorm(100, 1,2), simplify = FALSE)


Michael

On Sat, Dec 3, 2011 at 2:38 PM, andrija djurovic <[hidden email]> wrote:

> Hi Brad. Maybe something like this:
>
> lapply(rep(1,6), function(x) rnorm(10,0,1))
>
> Andrija
>
> On Sat, Dec 3, 2011 at 8:21 PM, B77S <[hidden email]> wrote:
>
>> Hi Michael,
>> How would you do this with lapply to return a list?
>> I can't seem to get that to work (I haven't used these much and am trying
>> to
>> learn).
>> Thanks
>> Brad
>>
>>
>> Michael Weylandt wrote
>> >
>> > ? replicate
>> >
>> > or a for loop
>> >
>> > or do all one hundred simulations at once
>> >
>> > x <- matrix(rnorm(100^2, 1, 2), 100)
>> >
>> > It's going to depend on what you want to do with the numbers.
>> >
>> > Michael
>> >
>> > On Sat, Dec 3, 2011 at 1:10 PM, Martin Zonyrah &lt;martin2005z@&gt;
>> wrote:
>> >> Hi,
>> >> I need help. I am trying to iterate this command  x <- rnorm(100, 1.0,
>> >> 2.0) one hundred times in R but I don't seem to have a clue.
>> >> Can anyone help?
>> >> Your help is very much appreciated.
>> >>
>> >> Martin
>> >>
>> >>        [[alternative HTML version deleted]]
>> >>
>> >>
>> >> ______________________________________________
>> >> R-help@ 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.
>> >>
>> >
>> > ______________________________________________
>> > R-help@ 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.
>> >
>>
>>
>> --
>> View this message in context:
>> http://r.789695.n4.nabble.com/Iteration-in-R-tp4154433p4154479.html
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> ______________________________________________
>> [hidden email] 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]]
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Iteration in R

Michael Weylandt
In reply to this post by Brad Schneid
Because lapply() tries to pass an argument to FUN and there's none
that it can receive.

This would work however:

lapply(rep(100, 6), rnorm, mean = 1, sd = 2)

Michael

On Sat, Dec 3, 2011 at 2:42 PM, B77S <[hidden email]> wrote:

> Interesting and thank you; I'm confused as to why this doesn't work with:
>
> lapply(rep(1,6), FUN=rnorm, n=10, mean=1.0, sd=1)
>
>
>
> andrija djurovic wrote
>>
>> Hi Brad. Maybe something like this:
>>
>> lapply(rep(1,6), function(x) rnorm(10,0,1))
>>
>> Andrija
>>
>> On Sat, Dec 3, 2011 at 8:21 PM, B77S &lt;bps0002@&gt; wrote:
>>
>>> Hi Michael,
>>> How would you do this with lapply to return a list?
>>> I can't seem to get that to work (I haven't used these much and am trying
>>> to
>>> learn).
>>> Thanks
>>> Brad
>>>
>>>
>>> Michael Weylandt wrote
>>> >
>>> > ? replicate
>>> >
>>> > or a for loop
>>> >
>>> > or do all one hundred simulations at once
>>> >
>>> > x <- matrix(rnorm(100^2, 1, 2), 100)
>>> >
>>> > It's going to depend on what you want to do with the numbers.
>>> >
>>> > Michael
>>> >
>>> > On Sat, Dec 3, 2011 at 1:10 PM, Martin Zonyrah &lt;martin2005z@&gt;
>>> wrote:
>>> >> Hi,
>>> >> I need help. I am trying to iterate this command  x <- rnorm(100, 1.0,
>>> >> 2.0) one hundred times in R but I don't seem to have a clue.
>>> >> Can anyone help?
>>> >> Your help is very much appreciated.
>>> >>
>>> >> Martin
>>> >>
>>> >>        [[alternative HTML version deleted]]
>>> >>
>>> >>
>>> >> ______________________________________________
>>> >> R-help@ 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.
>>> >>
>>> >
>>> > ______________________________________________
>>> > R-help@ 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.
>>> >
>>>
>>>
>>> --
>>> View this message in context:
>>> http://r.789695.n4.nabble.com/Iteration-in-R-tp4154433p4154479.html
>>> Sent from the R help mailing list archive at Nabble.com.
>>>
>>> ______________________________________________
>>> R-help@ 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]]
>>
>> ______________________________________________
>> R-help@ 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.
>>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Iteration-in-R-tp4154433p4154559.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.