Re: [R] Help with basic loop

From: Ivan Calandra <ivan.calandra_at_uni-hamburg.de>
Date: Mon, 11 Apr 2011 11:00:26 +0200

Well, I was quite blind not to change 1 to 1000 in runif() and use replicate()!!
It gets even faster if you create prob first.

Ivan

Le 4/11/2011 10:53, Dennis Murphy a écrit :
> Hi:
>
> Let's assume the lengths of each vector are the same so that they can
> be multiplied. Here's the timing on my machine:
>
> > system.time(replicate(1000, { prob<-numeric(1000)
> +
> + for (n in 1:1000) {
> + task1 <- runif(1, min=0.8, max= 0.9)
> + task2 <- runif(1, min=0.75, max= 0.85)
> + task3 <- runif(1, min=0.81, max= 0.89)
> + prob[n]<-task1*task2*task3
> + }
> + }))
> user system elapsed
> 16.96 0.01 17.19
> > system.time(replicate(1000, {
> + task1 = runif(1000, min = 0.8, max = 0.9)
> + task2 <- runif(1000, min = 0.75, max = 0.85)
> + task3 <- runif(1000, min = 0.81, max = 0.89)
> + prob <- task1 * task2 * task3 } ))
> user system elapsed
> 0.37 0.00 0.39
>
> Dennis
>
> On Mon, Apr 11, 2011 at 1:42 AM, Ivan Calandra
> <ivan.calandra_at_uni-hamburg.de <mailto:ivan.calandra_at_uni-hamburg.de>>
> wrote:
>
> Hi,
>
> I think you can do this without a loop (well, replicate() is based
> on sapply()):
> prob<-numeric(1000)
> task1 <- replicate(1000,runif(1, min=0.8, max= 0.9))
> task2 <- replicate(1000,runif(1, min=0.75, max= 0.85))
> task3 <- replicate(1000,runif(1, min=0.81, max= 0.89))
> prob <- task1*task2*task3
>
> It might not be faster, but I don't think it can be slower. And I
> find the code easier and clearer.
> Please correct me if this is not equivalent.
>
> HTH,
> Ivan
>
>
> Le 4/11/2011 01:06, Daniel Malter a écrit :
>
> The loop is correct, you just need to make sure that your
> result is computed
> and stored as the n-th element that is returned by the loop.
> Pick up any
> manual of R, and looping will be explained there. Also, I
> would recommend
> that you draw a random number for every iteration of the loop.
> Defining the
> random vectors outside the loop make sense to me only if they
> are the same
> length as n.
>
> prob<-numeric(1000)
>
> for (n in 1:1000) {
> task1<- runif(1, min=0.8, max= 0.9)
> task2<- runif(1, min=0.75, max= 0.85)
> task3<- runif(1, min=0.81, max= 0.89)
> prob[n]<-task1*task2*task3
> }
>
> If you wanted to store the individual probabilities
> (task1..3), you would
> proceed accordingly by defining them outside the loop and
> storing the value
> in the loop as the n-th element of that vector just like for prob.
>
> HTH,
> Daniel
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/Help-with-basic-loop-tp3440190p3440607.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help_at_r-project.org <mailto: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.
>
>
> --
> Ivan CALANDRA
> PhD Student
> University of Hamburg
> Biozentrum Grindel und Zoologisches Museum
> Abt. Säugetiere
> Martin-Luther-King-Platz 3
> D-20146 Hamburg, GERMANY
> +49(0)40 42838 6231 <tel:%2B49%280%2940%2042838%206231>
> ivan.calandra_at_uni-hamburg.de <mailto:ivan.calandra_at_uni-hamburg.de>
>
> **********
> http://www.for771.uni-bonn.de
> http://webapp5.rrz.uni-hamburg.de/mammals/eng/1525_8_1.php
>
>
> ______________________________________________
> R-help_at_r-project.org <mailto: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.
>
>

-- 
Ivan CALANDRA
PhD Student
University of Hamburg
Biozentrum Grindel und Zoologisches Museum
Abt. Säugetiere
Martin-Luther-King-Platz 3
D-20146 Hamburg, GERMANY
+49(0)40 42838 6231
ivan.calandra_at_uni-hamburg.de

**********
http://www.for771.uni-bonn.de
http://webapp5.rrz.uni-hamburg.de/mammals/eng/1525_8_1.php

______________________________________________
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.
Received on Mon 11 Apr 2011 - 09:02:34 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 11 Apr 2011 - 09:20:28 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive