← Back to R Course | Chapter 9: Statistics | Lesson 7 of 7

Probability Distributions

Probability distributions describe how likely different results are, like knowing that most people are about average height and few are extremely tall. R has built-in tools to draw values from them and work out the chances.
Syntax
r
dnorm(x, mean, sd)    # density
pnorm(q, mean, sd)    # cumulative probability
qnorm(p, mean, sd)    # quantile
rnorm(n, mean, sd)    # random values

Probability Distributions

R has built-in functions for common probability distributions, following a d/p/q/r naming convention -- rnorm() generates random normal values, dnorm() gives the density, pnorm() gives the cumulative probability, and qnorm() gives the quantile.

This same pattern applies to other distributions like runif(), rbinom(), and rpois(). This built-in statistical machinery is a major reason R is popular for data analysis.

Note:
  • The prefix tells you what the function does:
  • r=random draw
  • d=density
  • p=cumulative probability
  • q=quantile.

Example: Probability Distributions

markup
set.seed(1)
samples <- rnorm(5, mean = 0, sd = 1)
print(samples)

cat("P(X <= 0) for standard normal:", pnorm(0), "\n")
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Mixing up the prefixes, such as using dnorm for the probability up to a point, when d is the density, p the cumulative probability, q the quantile and r random draws.
  2. Forgetting sd is the standard deviation and passing a variance to rnorm(5, mean = 0, sd = 4).
  3. Expecting rnorm() to give the same numbers each run, when it needs set.seed().
Chapter Summary
  • Basic statistics such as mean, median, standard deviation, min, max, and range describe data.
  • summary() and cor() give overviews and correlations.
  • sample(), set.seed(), and probability distributions support random data.
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.