Home > database >  Loop over range of numbers for distribution in R
Loop over range of numbers for distribution in R

Time:02-05

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.

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!

CodePudding user response:

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

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

  •  Tags:  
  • r
  • Related