Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Loop over range of numbers for distribution in R

Sorry if this is a really basic question, but I just couldn’t figure it out on my own.

I’m currently learning R for college and I’m trying to figure out how to loop over a range of numbers and input them into a distribution function, like so:

for (i in 0:3) {
  dpois(i,2/3)
}

All I’m trying to accomplish here is for R to calculate a Poisson distribution for 0-3, with a lambda of 2/3.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

R expects some additional input which I haven’t quite figured out.
If I replace dpois with some other function (like print()), the loop works.

Any tips would be greatly appreciated.

Thanks!

>Solution :

R is already vectorized, and as such no for-loop is necessary.

dpois(0:3, 2/3) should do the trick.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading