Very simple loop

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

Very simple loop

Davg
I'm very new to R and am trying to create my first loop.

I have:

x <-c(0:200)
A <- dpois(x,exp(4.5355343))
B <- dpois(x,exp(4.5355343 + 0.0118638))
C <- dpois(x,exp(4.5355343  -0.0234615))
D <- dpois(x,exp(4.5355343 + 0.0316557))
E <- dpois(x,exp(4.5355343 + 0.0004716))
F <- dpois(x,exp(4.5355343 + 0.056437))
G <- dpois(x,exp(4.5355343 + 0.1225822))

and would like to to get A[K] + B[K] + C[K] + D[K] + E[K] + F[K] G[K]
for K(0:200)

And then plot these cumulative values.

Many thanks.
Reply | Threaded
Open this post in threaded view
|

Re: Very simple loop

Michael Weylandt
But the awesome thing is you don't need a for loop at all thanks to
the magic of R's vectorization!

This will do it (and much faster than an R level loop would):

x = 0:200 # Note that you don't need a c() since you aren't
concatenating 0:200 with anything
A <- dpois(x,exp(4.5355343))
B <- dpois(x,exp(4.5355343 + 0.0118638))
C <- dpois(x,exp(4.5355343  -0.0234615))
D <- dpois(x,exp(4.5355343 + 0.0316557))
E <- dpois(x,exp(4.5355343 + 0.0004716))
F <- dpois(x,exp(4.5355343 + 0.056437))
G <- dpois(x,exp(4.5355343 + 0.1225822))

Y = A + B + C + D+ E+ F + G

plot(x, Y)

I'd advise you to look around for a good R guide: (there are quite a
few free online, many tailored to specific disciplines) vectorization
is pretty sweet and if you don't get into it early, you'll find R to
be much more cumbersome and slow than it truly is.

Hope this helps,

Michael

On Mon, Nov 14, 2011 at 10:59 AM, Davg <[hidden email]> wrote:

> I'm very new to R and am trying to create my first loop.
>
> I have:
>
> x <-c(0:200)
> A <- dpois(x,exp(4.5355343))
> B <- dpois(x,exp(4.5355343 + 0.0118638))
> C <- dpois(x,exp(4.5355343  -0.0234615))
> D <- dpois(x,exp(4.5355343 + 0.0316557))
> E <- dpois(x,exp(4.5355343 + 0.0004716))
> F <- dpois(x,exp(4.5355343 + 0.056437))
> G <- dpois(x,exp(4.5355343 + 0.1225822))
>
> and would like to to get A[K] + B[K] + C[K] + D[K] + E[K] + F[K] G[K]
> for K(0:200)
>
> And then plot these cumulative values.
>
> Many thanks.
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Very-simple-loop-tp4039895p4039895.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.
Reply | Threaded
Open this post in threaded view
|

Re: Very simple loop

Sarah Goslee
In reply to this post by Davg
Hi,

On Mon, Nov 14, 2011 at 10:59 AM, Davg <[hidden email]> wrote:

> I'm very new to R and am trying to create my first loop.
>
> I have:
>
> x <-c(0:200)
> A <- dpois(x,exp(4.5355343))
> B <- dpois(x,exp(4.5355343 + 0.0118638))
> C <- dpois(x,exp(4.5355343  -0.0234615))
> D <- dpois(x,exp(4.5355343 + 0.0316557))
> E <- dpois(x,exp(4.5355343 + 0.0004716))
> F <- dpois(x,exp(4.5355343 + 0.056437))
> G <- dpois(x,exp(4.5355343 + 0.1225822))
>
> and would like to to get A[K] + B[K] + C[K] + D[K] + E[K] + F[K] G[K]
> for K(0:200)

R indexing starts with 1, so even though x starts with 0, the corresponding
index is 1:201

You don't need a loop at all: R can easily handle this with:

all.sum <- A + B + C + D + E + F + G

> And then plot these cumulative values.

What kind of plot? You might check out
?hist
and
?plot
for ideas.

Sarah
--
Sarah Goslee
http://www.functionaldiversity.org

______________________________________________
[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: Very simple loop

jholtman
In reply to this post by Davg
x <-c(0:200)
A <- dpois(x,exp(4.5355343))
B <- dpois(x,exp(4.5355343 + 0.0118638))
C <- dpois(x,exp(4.5355343  -0.0234615))
D <- dpois(x,exp(4.5355343 + 0.0316557))
E <- dpois(x,exp(4.5355343 + 0.0004716))
F <- dpois(x,exp(4.5355343 + 0.056437))
G <- dpois(x,exp(4.5355343 + 0.1225822))
total <- A + B + C + D + E + F + G
matplot(cbind(A, B, C, D, E, F, G, total))



On Mon, Nov 14, 2011 at 10:59 AM, Davg <[hidden email]> wrote:

> I'm very new to R and am trying to create my first loop.
>
> I have:
>
> x <-c(0:200)
> A <- dpois(x,exp(4.5355343))
> B <- dpois(x,exp(4.5355343 + 0.0118638))
> C <- dpois(x,exp(4.5355343  -0.0234615))
> D <- dpois(x,exp(4.5355343 + 0.0316557))
> E <- dpois(x,exp(4.5355343 + 0.0004716))
> F <- dpois(x,exp(4.5355343 + 0.056437))
> G <- dpois(x,exp(4.5355343 + 0.1225822))
>
> and would like to to get A[K] + B[K] + C[K] + D[K] + E[K] + F[K] G[K]
> for K(0:200)
>
> And then plot these cumulative values.
>
> Many thanks.
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Very-simple-loop-tp4039895p4039895.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.
>



--
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.

______________________________________________
[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: Very simple loop

Joshua Wiley-2
In reply to this post by Davg
x <-c(0:200)
dat <- data.frame(
  A = dpois(x,exp(4.5355343)),
  B = dpois(x,exp(4.5355343 + 0.0118638)),
  C = dpois(x,exp(4.5355343  -0.0234615)),
  D = dpois(x,exp(4.5355343 + 0.0316557)),
  E = dpois(x,exp(4.5355343 + 0.0004716)),
  F = dpois(x,exp(4.5355343 + 0.056437)),
  G = dpois(x,exp(4.5355343 + 0.1225822)))

## using a looping approach
## instantiate a vector to hold results
results <- vector("numeric", length = length(x))

for(i in 1:201) {# R starts indexing at 1, not 0
  results[i] <- dat[i, "A"] + dat[i, "B"] + dat[i, "C"] +
    dat[i, "D"] + dat[i, "E"] + dat[i, "F"] + dat[i, "G"]
}

## find and plot cumulatively values
plot(x, cumsum(results))

You may be wondering why I put all the variables in a data frame.  It
is because it will be much easier in the long run.  This accomplishes
the same thing as the loop, with a fraction of the effort and much
much faster (loops can be slow in R, and vectorizing is preferred).

plot(x, cumsum(rowSums(dat)))

rowSums() is a vectorized function that finds the (duh) sums of each
row, then I just find the cumulative sum, and plot.

Hope this helps,

Josh

On Mon, Nov 14, 2011 at 7:59 AM, Davg <[hidden email]> wrote:

> I'm very new to R and am trying to create my first loop.
>
> I have:
>
> x <-c(0:200)
> A <- dpois(x,exp(4.5355343))
> B <- dpois(x,exp(4.5355343 + 0.0118638))
> C <- dpois(x,exp(4.5355343  -0.0234615))
> D <- dpois(x,exp(4.5355343 + 0.0316557))
> E <- dpois(x,exp(4.5355343 + 0.0004716))
> F <- dpois(x,exp(4.5355343 + 0.056437))
> G <- dpois(x,exp(4.5355343 + 0.1225822))
>
> and would like to to get A[K] + B[K] + C[K] + D[K] + E[K] + F[K] G[K]
> for K(0:200)
>
> And then plot these cumulative values.
>
> Many thanks.
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Very-simple-loop-tp4039895p4039895.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.
>



--
Joshua Wiley
Ph.D. Student, Health Psychology
Programmer Analyst II, ATS Statistical Consulting Group
University of California, Los Angeles
https://joshuawiley.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.
Reply | Threaded
Open this post in threaded view
|

Re: Very simple loop

ELCano
In reply to this post by Davg
Dear David,
You do not need a loop. The vectors are equaly sized, so sum them and then
plot the vector with the sums:

total <- A+B+C+D+E+F+G
plot (total, type="l")

Regards,
Emilio

2011/11/14 Davg <[hidden email]>

> I'm very new to R and am trying to create my first loop.
>
> I have:
>
> x <-c(0:200)
> A <- dpois(x,exp(4.5355343))
> B <- dpois(x,exp(4.5355343 + 0.0118638))
> C <- dpois(x,exp(4.5355343  -0.0234615))
> D <- dpois(x,exp(4.5355343 + 0.0316557))
> E <- dpois(x,exp(4.5355343 + 0.0004716))
> F <- dpois(x,exp(4.5355343 + 0.056437))
> G <- dpois(x,exp(4.5355343 + 0.1225822))
>
> and would like to to get A[K] + B[K] + C[K] + D[K] + E[K] + F[K] G[K]
> for K(0:200)
>
> And then plot these cumulative values.
>
> Many thanks.
>
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/Very-simple-loop-tp4039895p4039895.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.
>


--
_____________________________________

*Emilio López Cano*
+34 665 676 225
Department of Statistics ans Operations Research
Universidad Rey Juan Carlos <http://www.urjc.es>

<http://www.urjc.es>
<http://www.urjc.es>

        [[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: Very simple loop

Davg
In reply to this post by Davg
Thank you all!

It's working perfectly.  I will have a look for an online guide.
Reply | Threaded
Open this post in threaded view
|

Re: Very simple loop

Michael Weylandt
The one included in the standard R installation -- which can be
accessed by typing help.start() at your prompt -- is quite good for
beginners (and very conveniently located). If you tell us a bit more
about yourself, we can help direct you to others as well:
specifically,

1) Prior programming experience?
2) Experience with other statistical packages?
3) Is there a specific domain you will be working in? E.g.,
biomedical, time series/financial, simulation, etc. There exist
specialized intros for many branches of stats
4) Level of statistical training: there are some books that provide an
integrated intro to statistical methods and R so if you need to brush
up your stats as well, you can do them both at once.

Best of luck and welcome to the R-world -- it's quite a fun place,

Michael

On Mon, Nov 14, 2011 at 12:38 PM, Davg <[hidden email]> wrote:

> Thank you all!
>
> It's working perfectly.  I will have a look for an online guide.
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Very-simple-loop-tp4039895p4040291.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.
Reply | Threaded
Open this post in threaded view
|

Re: Very simple loop

John Kane-2
In reply to this post by Davg
As others have said, no need for a loop
Another approach

mydata <- data.frame(A = dpois(x,exp(4.5355343)),
B = dpois(x,exp(4.5355343 + 0.0118638)),
C = dpois(x,exp(4.5355343  -0.0234615)),
D = dpois(x,exp(4.5355343 + 0.0316557)),
E = dpois(x,exp(4.5355343 + 0.0004716)),
F = dpois(x,exp(4.5355343 + 0.056437)),
G = dpois(x,exp(4.5355343 + 0.1225822)))

df1 <- rowSums(mydata)
plot(df1)

--- On Mon, 11/14/11, Davg <[hidden email]> wrote:

> From: Davg <[hidden email]>
> Subject: [R] Very simple loop
> To: [hidden email]
> Received: Monday, November 14, 2011, 10:59 AM
> I'm very new to R and am trying to
> create my first loop.
>
> I have:
>
> x <-c(0:200)
> A <- dpois(x,exp(4.5355343))
> B <- dpois(x,exp(4.5355343 + 0.0118638))
> C <- dpois(x,exp(4.5355343  -0.0234615))
> D <- dpois(x,exp(4.5355343 + 0.0316557))
> E <- dpois(x,exp(4.5355343 + 0.0004716))
> F <- dpois(x,exp(4.5355343 + 0.056437))
> G <- dpois(x,exp(4.5355343 + 0.1225822))
>
> and would like to to get A[K] + B[K] + C[K] + D[K] + E[K] +
> F[K] G[K]
> for K(0:200)
>
> And then plot these cumulative values.
>
> Many thanks.
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Very-simple-loop-tp4039895p4039895.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.