📚 R Programming Tutorial

R Programming

Learn R programming concepts with simple explanations, examples, techniques and practical notes.

← Back to R Programming

Functions for Statistical Distributions

In R, you can work with statistical distributions using a variety of built-in functions. Each distribution has four functions, whose names start with a letter that tells what the function does: d for the density (or probability mass), p for the cumulative probability, q for the quantile and r for random numbers. Below are the key functions for some common distributions:

Distribution Density/pmf cdf Quantiles Random Numbers
Normal dnorm() pnorm() qnorm() rnorm()
Chi square dchisq() pchisq() qchisq() rchisq()
Binomial dbinom() pbinom() qbinom() rbinom()

Normal Distribution Functions

dnorm()

The dnorm(x, mean, sd) function returns the height of the normal distribution curve (i.e., probability density) at point x.

Example: Normal Distribution with mean = 100 and sd = 15

> dnorm(100, mean = 100, sd = 15)
[1] 0.02659615

It checks the density at the mean, where the bell curve is tallest.

pnorm()

The pnorm(x, mean, sd) function gives the cumulative probability that a normally distributed random variable is less than or equal to x.

Example: Normal Distribution with mean = 100 and sd = 15

> pnorm(110, mean = 100, sd = 15)
[1] 0.7475075

About 74.75% of values are below 110.

qnorm()

The qnorm(p, mean, sd) function returns the quantile (or cut-off value) for a given cumulative probability p.

Example: Normal Distribution with mean = 100 and sd = 15

> qnorm(0.90, mean = 100, sd = 15)
[1] 119.2233

90% of the students scored below 119.23.

rnorm()

The rnorm(n, mean, sd) function generates n random values from a normal distribution with the given mean and standard deviation.

Example: Normal Distribution with mean = 100 and sd = 15

> rnorm(5, mean = 100, sd = 15)
[1] 108.17404  79.47250 107.41886  77.67176 120.89173

These could represent randomly generated student test scores.

Note: The values returned by rnorm() are random, so the output shown is only a sample and will be different each time. Use set.seed() before the call to get the same values every time.

Chi square Distribution Functions

dchisq()

The dchisq(x, df) function returns the density (height) of the Chi-square distribution at a value x, with df = degrees of freedom.

Example: Density at x = 2 with df = 3

> dchisq(2, df = 3)
[1] 0.2075537

This means the value of the Chi-square PDF at x = 2 is about 0.2076 when degrees of freedom = 3.

pchisq()

The pchisq(q, df) function gives the cumulative probability for the Chi-square distribution.

Example: Probability X ≤ 4 with df = 3

> pchisq(4, df = 3)
[1] 0.7385359

There is a 73.85% chance that a Chi-square random variable with df = 3 is less than or equal to 4.

qchisq()

The qchisq(p, df) function returns the Chi-square value x such that P(X ≤ x) = p, where X follows a Chi-square distribution with df degrees of freedom.

Example: 95th percentile (critical value) for df = 4

> qchisq(0.95, df = 4)
[1] 9.487729

The value 9.49 is the cut-off where 95% of values lie below it (i.e., P(X ≤ 9.49) = 0.95 for df = 4).

rchisq()

The rchisq(n, df) function generates n random numbers from a Chi-square distribution with df degrees of freedom.

Example: Generate 5 random Chi-square values with df = 4

> rchisq(5, df = 4)
[1] 0.7686753 9.2089134 0.1694429 2.9136730 1.2949039

These values are random samples from a Chi-square distribution.

Note: The values returned by rchisq() are random, so the output shown is only a sample and will be different each time. Use set.seed() before the call to get the same values every time.

Binomial Distribution Functions

dbinom()

The dbinom(x, size, prob) function returns the probability of getting exactly x successes in size trials, where each trial has a success probability of prob.

Example: P(X = 6) for 10 fair coin flips

> dbinom(6, size = 10, prob = 0.5)
[1] 0.2050781

So there is about a 20.5% chance of getting exactly 6 heads.

pbinom()

The pbinom(q, size, prob) function gives the probability of getting ≤ q successes in size trials, where the probability of success in each trial is prob.

Example: P(X ≤ 6) in 10 coin flips (p = 0.5)

> pbinom(6, size = 10, prob = 0.5)
[1] 0.828125

There is about an 82.81% chance of getting 6 or fewer heads in 10 coin tosses.

qbinom()

The qbinom(p, size, prob) function returns the smallest number of successes x such that the cumulative probability P(X ≤ x) is at least p, when performing size trials with success probability prob.

Example: 90th percentile for a binomial process

> qbinom(0.90, size = 15, prob = 0.3)
[1] 7

This gives the minimum number of defective items for which the cumulative probability reaches 90%.

rbinom()

The rbinom(n, size, prob) function returns a vector of random numbers, where each number represents the count of successes in size trials.

Example:

> results <- rbinom(n = 5, size = 10, prob = 0.5)
> print(results)
[1] 4 3 6 4 5

Each value in results indicates the number of heads obtained in 10 coin flips in that experiment. For instance, if one of the outcomes is 6, it means in that experiment 6 heads came up out of 10 flips.

Note: The values returned by rbinom() are random, so the output shown is only a sample and will be different each time. Use set.seed() before the call to get the same values every time.