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.
In this page:
Syntax
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
set.seed(1)
samples <- rnorm(5, mean = 0, sd = 1)
print(samples)
cat("P(X <= 0) for standard normal:", pnorm(0), "\n")
Login to try C/C++/Java/PHP code in the editor
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
- Mixing up the prefixes, such as using
dnormfor the probability up to a point, whendis the density,pthe cumulative probability,qthe quantile andrrandom draws. - Forgetting
sdis the standard deviation and passing a variance tornorm(5, mean = 0, sd = 4). - Expecting
rnorm()to give the same numbers each run, when it needsset.seed().
Chapter Summary
- Basic statistics such as mean, median, standard deviation, min, max, and range describe data.
summary()andcor()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: