If p is the probability of success of a binomial trial i would like to calculate the number of trials n required that if performed would give a probability x of at least one success. Is there a way to obtain this n in R? I have a large vector of probabilities and would like to extract this n for a given x.

Thank you


2 Answers 2


The probability of at least one success, x, is 1-p(no successes), which is equal to 1-p(fail)^n, which in turn is equal to 1-(1-p(success))^n.

So, to rearrange this to get n = log(1-x)/log(1-p)

p <- c(.2,.3,.5)
x <- 0.5
n <- log(1-x)/log(1-p)

Of course you can only have an integer number of trials, so you need to use ceiling on this:

[1] 4 2 1

For n trials, and probability p, the number of expected successes is n*p. So, just rearrange the terms. If expected successes = 1, then 1=n*p, so n=1/p. This can be easily calculated in R:

probabilities = c(.2,.3,.5)
  • $\begingroup$ The expected value is correct, but it does not answer the question! $\endgroup$
    – whuber
    Mar 7, 2012 at 23:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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