# A Guide to dbinom, pbinom, qbinom, and rbinom in R

• Last Updated : 23 May, 2022

In this article, we will be looking at a guide to the dbinom, pbinom, qbinom, and rbinom methods of the binomial distribution in the R programming language.

## dbinom function

This function returns the value of the probability density function (pdf) of the binomial distribution given a certain random variable x, number of trials (size), and probability of success on each trial (prob).

Syntax: dbinom(x, size, prob)

Parameters:

• x : a vector of numbers.
• size : the number of trials.
• prob : the probability of success of each trial.

The dbinom function is also used to get the probability of getting a certain number of successes (x) in a certain number of trials (size) where the probability of success on each trial is fixed (prob).

Example 1:

Here, we are using the example to the binomial distribution to find the probability where a person makes 70% of his throw attempts, and if he shoots 20 throws, so what will be the probability that the person makes exactly 12 of them attempt, so here we are simply using the dbinom function and passing the given statements properties as its parameters and further getting the result in the R. function

## R

 `dbinom``(x=12, size=20, prob=.7)`

Output:

`[1] 0.1143967`

Example 2:

In this example, we are simply calculating the probability to get the heads exactly 17 times if the coin is tossed 50 times fairly using the dbinom function. Since the coin is tossed fairly the prob parameter to the function is passed to be 0.5.

## R

 `dbinom``(x=17, size=50, prob=.5)`

Output:

`[1] 0.00874623`

## pbinom function

This function returns the value of the cumulative density function (cdf) of the binomial distribution given a certain random variable q, number of trials (size), and probability of success on each trial (prob).

Syntax: pbinom(x, size, prob)

Parameters:

• x: a vector of numbers.
• size: the number of trials.
• prob: the probability of success of each trial.

pbinom function returns the area to the left of a given value q in the binomial distribution. If you’re interested in the area to the right of a given value q, you can simply add the argument lower.tail = FALSE

Syntax:

`pbinom(q, size, prob, lower.tail = FALSE) `

Example 1:

Under this example, we are calculating the probability to get a head more than 3 times if the coin is flipped fairly 10 times using the pbinom() function. Since the coin is tossed fairly the prob parameter to the function is passed to be 0.5.

## R

 `pbinom``(3, size=10, prob=.5, lower.tail=``FALSE``)`

Output:

`[1] 0.828125`

Example 2:

In this example, we are calculating the probability if a man scores a strike on 30% of his attempts when he bowls, If he bowls 50 times, what is the probability that he scores 30 or fewer strikes using the pbinom() function in R.

## R

 `pbinom``(30, size=50, prob=.7)`

Output:

`[1] 0.0848026`

## qbinom function

This function returns the value of the inverse cumulative density function (cdf) of the binomial distribution given a certain random variable q, number of trials (size), and probability of success on each trial (prob). And with the use of this function, we can find out the pth quantile of the binomial distribution.

Syntax: qbinom(q, size, prob)

Parameters:

• x: a vector of numbers.
• size: the number of trials.
• prob: the probability of success of each trial.

Example 1:

Under this example, we are using the qbinorm function to get the 19th quantile of a binomial distribution with 30 trials with the prob of success to 0.6.

## R

 `qbinom``(.19, size=30, prob=.6)`

Output:

`[1] 16`

## rbinom function

This function generates a vector of binomial distributed random variables given a vector length n, number of trials (size), and probability of success on each trial (prob).

Syntax: rbinom(n, size, prob)

Parameters:

• n: number of observations.
• size: the number of trials.
• prob: the probability of success of each trial.

Example:

In this example, we are generating a vector with a number of successes of 500 binomial experiments including the 90 trials where the probability of success on each trial is 0.7 and then verifying it by using calculating the mean of the generated vector using the rnorm function in the R.

## R

 `gfg <- ``rbinom``(500, size=100, prob=.6)`   `mean``(gfg)`

Output:

`[1] 60.01`

Note: The more random variables we create, the closer the mean number of successes is to the expected number of successes. As the “Expected number of successes” = n*p where n is the number of trials and p is the probability of success on each trial.

My Personal Notes arrow_drop_up
Related Articles