Confidence Interval Exercises in R: 10 Problems with Full Solutions
These 10 confidence interval exercises in R walk from a one-line t.test() through manual qt() formulas, two-sample and paired intervals, bootstrap CIs for the median, regression coefficient intervals, and the sample-size-versus-width trade-off, with full runnable solutions next to every problem.
How do you build a 95% confidence interval in R?
R already hands you a confidence interval every time you run t.test(), but most learners skim past that conf.int line without realising it is the whole point. Here is a one-liner on mtcars$mpg that returns a 95% interval for the true mean mpg, so you see exactly where the interval lives in the output before the exercises start asking you to build intervals yourself.
The two numbers 17.92 and 22.26 are the lower and upper bounds of a 95% CI for the mean mpg across all cars in the population mtcars was drawn from. If you repeated this study many times, about 95% of such intervals would cover the true mean. The sample mean mean(mtcars$mpg) is 20.09, which sits right between them, and the interval runs roughly 2.2 units either side of that centre.
print() output of t.test() is noisy with null hypothesis machinery. Extracting the interval as a 2-element vector keeps your code terse and makes it easy to round(), diff(), or pass to another function.The formula behind that t.test() call is the same one every introductory stats course uses:
$$\bar{x} \pm t_{\alpha/2,\,n-1} \cdot \frac{s}{\sqrt{n}}$$
Where:
- $\bar{x}$ = sample mean
- $s$ = sample standard deviation
- $n$ = sample size
- $t_{\alpha/2,\,n-1}$ = the t critical value for confidence level $1-\alpha$ and $n-1$ degrees of freedom
You will reconstruct this formula by hand in Exercise 3.
Try it: Compute the 95% confidence interval for iris$Sepal.Length. Save the lower bound to ex_lower.
Click to reveal solution
Explanation: t.test() returns a list with a $conf.int field. Indexing with [1] grabs the lower bound; [2] would give the upper bound.
Which R function computes which type of confidence interval?
Not every confidence interval comes from t.test(). R has a small, well-chosen set of functions that each cover one family of estimators. Knowing which function to reach for is half the battle, so here are the four most common in one place with toy data.
Each call returns a two-element vector (or matrix, for regression), and every interval is built from the same pattern: a point estimate plus or minus a critical value times a standard error. The functions differ only in which critical value (z, t, or bootstrap quantile) and which standard error formula apply to the estimator at hand.
| You want a CI for... | Use | Key argument |
|---|---|---|
| A mean | t.test(x) |
conf.level |
| A difference of means | t.test(x, y) |
var.equal |
| Paired differences | t.test(x, y, paired = TRUE) |
(none) |
| A proportion | prop.test(x, n) |
correct |
| Regression coefficients | confint(lm_fit) |
level |
| A correlation | cor.test(x, y) |
(none) |
| A median (nonparametric) | replicate() + sample() + quantile() |
bootstrap reps |
Try it: You ran an A/B test and 92 of 150 users clicked. Which R function computes the 95% CI for the click-through rate? Set ex_fn_choice to one of "t.test", "prop.test", "confint", or "cor.test".
Click to reveal solution
Explanation: You are estimating a single proportion (clicks out of impressions), so prop.test(92, 150) is the tool. t.test() is for means, confint() is for regression coefficients, and cor.test() is for correlations.
Practice Exercises
Ten problems, roughly in order of difficulty. Each capstone uses my_ prefixed variables so your solutions never clash with the tutorial examples above.
Exercise 1: CI for a single mean
Build a 95% confidence interval for the mean mpg in mtcars using t.test(). Store the two-element interval in my_mpg_ci and print it.
Click to reveal solution
Explanation: t.test() by default builds a 95% confidence interval. The $conf.int field is a length-2 numeric vector with the lower and upper bounds. No null hypothesis is being tested here, you are just using t.test() as a convenient CI calculator.
Exercise 2: CI for a proportion
A landing page shows an ad to 150 visitors and 92 of them click. Build a 95% CI for the true click-through rate using prop.test() and store it in my_click_ci.
Click to reveal solution
Explanation: prop.test() uses the Wilson score interval by default, which is safer than the textbook Wald interval for small samples or extreme proportions. The point estimate is 92/150 = 0.613, and the CI runs from about 0.53 to 0.69.
Exercise 3: Manual CI using qt()
Compute the 95% CI for the mean of iris$Sepal.Length by hand using mean(), sd(), sqrt(), and qt(). Store your manual answer in my_manual_ci and the t.test() answer in my_auto_ci. Confirm both match to four decimals.
Click to reveal solution
Explanation: The formula mean plus or minus $t_{0.975,\,n-1} \cdot s/\sqrt{n}$ is what t.test() computes internally. qt(0.975, df = n-1) returns the critical value that cuts off 2.5% of the upper tail of a t distribution, matching a 95% two-sided CI. Both results agreeing to four decimals confirms the formula.
Exercise 4: CI for a difference of means
Build a 95% CI for the difference in mean mpg between 4-cylinder and 8-cylinder cars in mtcars. Store it in my_diff_ci. Does the interval exclude zero?
Click to reveal solution
Explanation: t.test(x, y) runs a Welch two-sample test by default (no equal-variance assumption) and returns $conf.int for the difference mean(x) - mean(y). The interval 8.32 to 15.08 is well above zero, so 4-cylinder cars genuinely get better mileage than 8-cylinder cars in this dataset, and you can be 95% confident the true mean difference is between about 8 and 15 mpg.
Exercise 5: Paired-sample CI
The built-in sleep dataset records hours of extra sleep for the same 10 patients under two drugs. Build a 95% CI for the mean difference (drug 1 minus drug 2) using a paired t-test. Store it in my_paired_ci.
Click to reveal solution
Explanation: paired = TRUE tells t.test() that each row in g1 corresponds to the same patient in g2. The CI is for the mean within-patient difference, not the between-group difference, which would ignore the pairing and give a wider interval. Because both bounds are negative, drug 2 outperforms drug 1 at the 95% confidence level.
Exercise 6: CI width vs confidence level
Compute 90%, 95%, and 99% CIs for the mean of mtcars$mpg. Store the three widths (upper minus lower) in a named numeric vector my_widths. Observe how the width grows with confidence level.
Click to reveal solution
Explanation: The 99% CI is almost 60% wider than the 90% CI, yet the sample has not changed. More confidence costs precision. This is why 95% survives as the default compromise, and why chasing 99.9% reliably can wreck a study's usefulness without a much larger sample.
Exercise 7: Bootstrap CI for the median
t.test() will not give you a CI for the median. Use a non-parametric bootstrap instead: resample mtcars$mpg with replacement, compute the median each time, and take the 2.5% and 97.5% quantiles. Store the interval in my_boot_ci. Set the seed to 2026 for reproducibility.
Click to reveal solution
Explanation: The percentile bootstrap approximates the sampling distribution of the median by resampling with replacement from the data 2,000 times. The 2.5% and 97.5% quantiles of the resampled medians bracket 95% of the resampling distribution. This same recipe works for any statistic (trimmed means, ratios, odds) whenever no neat closed-form CI exists.
Exercise 8: CI for regression coefficients
Fit lm(mpg ~ wt, data = mtcars) and extract the 95% confidence intervals for both the intercept and slope using confint(). Store the model in my_lm_fit and the CI matrix in my_coef_ci. Does the slope CI exclude zero?
Click to reveal solution
Explanation: confint() builds a CI for each coefficient using the fitted model's estimated standard errors and a t distribution with n - p degrees of freedom. The slope CI is [-6.49, -4.20], which strictly excludes zero, so wt is a statistically significant predictor of mpg at the 5% level. The intercept CI tells you about the implied mpg when wt = 0 (an extrapolation, but that is the model's statement).
Exercise 9: Sample size vs CI width
Generate samples of sizes $n = 25, 100, 400$ from $\mathcal{N}(100, 15^2)$, build 95% CIs for the mean at each sample size, and record the widths in a named vector my_widths_vs_n. Verify that each four-fold increase in $n$ roughly halves the width.
Click to reveal solution
Explanation: Going from n = 25 to n = 100 (a 4x increase) roughly halves the width (14.16 to 5.74). Going from 100 to 400 halves it again (5.74 to 3.05). That is the $1/\sqrt{n}$ law in action, which is why sample size planning uses squared width targets, not linear ones.
Exercise 10: CI for a correlation
Build a 95% CI for the Pearson correlation between mtcars$mpg and mtcars$wt using cor.test(). Store it in my_cor_ci. Does it cross zero?
Click to reveal solution
Explanation: cor.test() uses Fisher's z transform to build a CI for the correlation coefficient, then back-transforms to the $[-1, 1]$ scale. The point estimate is cor(mtcars$mpg, mtcars$wt) = -0.868, and the CI [-0.934, -0.744] is firmly negative, confirming a strong inverse relationship between weight and mileage.
Complete Example: End-to-end CI analysis of iris
Putting the pieces together on the iris dataset. Four short steps build four different kinds of confidence interval, each answering a different kind of question about the same 150 flowers.
The 95% CI for mean petal length is roughly 3.47 cm to 4.05 cm, a fairly tight interval because the dataset has 150 observations and moderate variability.
About one-third of flowers are virginica, and the CI [0.268, 0.408] reflects genuine sampling uncertainty at n = 150. If the dataset were perfectly balanced at 50/50/50, the true proportion is 1/3 = 0.333, which falls comfortably inside this interval.
The median petal length is less sensitive to outliers than the mean. The bootstrap CI [4.00, 4.50] is narrow and sits above the mean CI from Step 1 because petal length is right-skewed across species.
For every extra cm of sepal length, petal length grows by 1.73 cm to 2.11 cm (95% confidence). The slope interval excludes zero by a wide margin, so the relationship is clearly real. Four different estimators, four different CI recipes, one dataset, and each interval tells you something different about the same flowers.
Summary
Every confidence interval in R follows the same underlying logic, but the function you use depends on the estimator. Keep this table near the keyboard.
| CI for... | R call | Returns |
|---|---|---|
| Mean, one sample | t.test(x) |
$conf.int |
| Mean, two samples | t.test(x, y) |
$conf.int |
| Mean, paired | t.test(x, y, paired = TRUE) |
$conf.int |
| Proportion | prop.test(x, n) |
$conf.int |
| Median (nonparametric) | replicate(B, median(sample(x, TRUE))) + quantile(...) |
percentile CI |
| Regression coefficients | confint(lm_fit) |
2-column matrix |
| Correlation | cor.test(x, y) |
$conf.int |
Takeaways:
t.test()andconfint()together cover 80% of the CIs you will ever need.- Width shrinks like $1/\sqrt{n}$, so halving a CI costs 4x more data.
- Reach for the bootstrap when the statistic is nonstandard (median, trimmed mean, custom ratio).
- Confidence level trades width for coverage: 99% CIs are about 30% wider than 95% CIs on the same sample.
- Always report the interval, not just the point estimate. The CI communicates both effect size and precision in a single object.
References
- R Core Team. *
t.test()reference*. stat.ethz.ch - R Core Team. *
prop.test()reference*. stat.ethz.ch - R Core Team. *
confint()reference*. stat.ethz.ch - Wasserman, L. All of Statistics. Springer (2004), Chapter 6: Models, Statistical Inference and Learning.
- Efron, B. and Tibshirani, R. An Introduction to the Bootstrap. Chapman & Hall/CRC (1993).
- OpenStax. Introductory Statistics, Chapter 8: Confidence Intervals. openstax.org
- Statistics LibreTexts. 8.E: Confidence Intervals (Exercises). stats.libretexts.org/08:_Confidence_Intervals/8.E:_Confidence_Intervals_(Exercises))
Continue Learning
- Confidence Intervals in R, the parent explainer covers what a CI is, what it is not, and when the textbook definition trips people up.
- t-Test Exercises in R, sister practice set that exercises the other side of
t.test(), the p-value and the null hypothesis framing. - Hypothesis Testing Exercises in R, drill the decision framework that shares a backbone with every CI on this page.