Tell me more ×
Cross Validated is a question and answer site for statisticians, data analysts, data miners and data visualization experts. It's 100% free, no registration required.

I am not sure if this is an instance of vectorizing the operations in R, but this is where I am stuck:

I want to get:

dpois(1, 0.1)
dpois(2, 0.2)
dpois(3, 0.3)

and I tried:

dpois(1:3, 0.1:0.3)

and

do.call(dpois, list(x = 1:3, lambda = 0.1:0.3))

both do not work.

It there a R-ish way of doing this?

share|improve this question
This isn't really about vectorizing calls to dpois. It's more a question about creating vectors. – ashaw Feb 28 '11 at 16:49

1 Answer

up vote 6 down vote accepted

From help(dpois) it looks like you need x and lambda to be vectors (read more about object classes in the R Intro or any other R documentation to understand what this means).

The following works:

dpois(1:3, c(seq(0.1, 0.3, .1)))

Your first attempt fails because you are not concatenating (see: help(c)) the values for 0.1:0.3 into a vector and you are not providing any way for R to know what you want it to do with 0.1:0.3. Calling seq() in the manner above tells it to get a sequence from 0.1 to 0.3 by 0.1.

Your second attempt is pretty far into the weeds. There's no way you need a power-tool like do.call for this kind of thing.

share|improve this answer
I should have looked at this carefully. – suncoolsu Feb 28 '11 at 16:59
no worries, glad I could answer the question. – ashaw Feb 28 '11 at 17:03
The only problem is with the 0.1:0.3 part. 1:3 is already a vector and doesn't require concatenation. – Fojtasek Feb 28 '11 at 17:08
indeed. edited accordingly. – ashaw Feb 28 '11 at 17:15
4  
A more compact version of the same answer: dpois(1:3, 1:3/10). – Joshua Ulrich Feb 28 '11 at 18:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.