In this post, I’ll show you step by step how to conduct a t-test with only one sample in R—a so-called one-sample t-test. It is suitable whenever you want to compare the mean of a single group with a specific reference value.
What is the purpose of a one-sample t-test?
Imagine you want to check whether students at your university really sleep the recommended 7 hours on average. You collect the actual sleep duration of 30 people. Now you want to know: Is the mean significantly different from 7?
Definition:
A one-sample t-test compares the mean of a sample with a specified theoretical value $\mu_0$.
The null hypothesis is: $H_0: \mu = \mu_0$
The alternative hypothesis is: $H_1: \mu e \mu_0$ (two-tailed)
(Or $H_1: \mu \mu_0$ for a one-tailed test)
Example: Students’ sleep duration
Let’s simulate a small example. Suppose you have the following sleep data (in hours):
# Generate example data
sleep <- c(6.5, 7.0, 6.8, 7.1, 6.9, 6.2, 6.4, 6.7, 6.5, 7.0,
6.6, 6.8, 7.2, 6.5, 6.9, 7.1, 6.4, 6.8, 6.7, 6.9)
Now you want to test whether the average differs significantly from 7 hours.
Conducting the test in R
# t-test against a mean of 7
t.test(sleep, mu = 7)
Here you specify the variable (schlaf) and the reference value (mu = 7). By default, a two-sided test is performed.
Example output (abridged)
One Sample t-test
data: schlaf
t = -3.028, df = 19, p-value = 0.0068
alternative hypothesis: true mean is not equal to 7
95 percent confidence interval:
6.60 6.87
sample estimates:
mean of x
6.735
Interpretation
| Element | Meaning |
|---|---|
| t = -3.028 | The test statistic – the deviation in standard error units |
| df = 19 | Degrees of freedom (n – 1) |
| p-value = 0.0068 | Probability of observing a result this extreme (or more extreme) if $H_0$ is true |
| CI = [6.60, 6.87] | 95 % confidence interval for the mean |
| mean = 6.735 | The actual mean in your sample |
Conclusion: Since the p-value is less than 0.05, we reject $H_0$. The students sleep significantly less than 7 hours.
One-sided test
If you have a directional hypothesis (e.g., “Students sleep less than 7 hours”), you can perform the test as a one-sided test:
t.test(schlaf, mu = 7, alternative = "less")
Or:
t.test(schlaf, mu = 7, alternative = "greater")
Check normality
Especially with small samples, you should check whether the data are approximately normally distributed.
# Histogram + density
hist(schlaf, breaks = 8, probability = TRUE, col = "lightblue", main = "Distribution of sleep duration")
lines(density(schlaf), col = "red", lwd = 2)
# Q-Q plot
qqnorm(schlaf)
qqline(schlaf, col = "blue")
If the histogram is symmetrical and the points in the Q-Q plot lie approximately on the line, the assumption is justified.
Summary
| Step | Action |
|---|---|
| 1 | Collect data (metric variable) |
| 2 | Define the objective (comparison with a fixed value) |
| 3 | Conduct the test using t.test() |
| 4 | Interpret the result (p-value, confidence interval) |
| 5 | Optional: Check normality |
Q&A to think through
Question 1: What does the one-sample t-test test?
Answer: Whether the mean of a sample differs significantly from a specified reference value.
Question 2: When is the test informative?
Answer: When the variable is metric and the data are (approximately) normally distributed, especially for small samples.
Question 3: How is the test carried out in R?
Answer: Using t.test(variable, mu = gewünschter_Wert)
Question 4: What does a p-value smaller than 0.05 mean?
Answer: The observed difference is statistically significant; we reject the null hypothesis.
