A one-sample t-test is a statistical procedure used to determine whether a sample of observations could have been generated by a process with a specific mean. It is a parametric test and assumes that the data follows a normal distribution, has a known or an estimated standard deviation, and that the sample is randomly selected.
The null hypothesis of the t-test is that the population mean is equal to a specified value. The alternative hypothesis is that the population mean is not equal to the specified value.
In this article, we’ll walk through how to perform a one-sample t-test in R. We will cover everything from preparing your dataset, to performing the t-test and interpreting the results.
Preparing Your Dataset
The first step in performing a one-sample t-test is preparing your dataset. This often involves loading your data into R, cleaning it, and ensuring that it meets the assumptions of the test.
You can load your data into R using various functions like read.csv()
, read.xlsx()
, etc., depending on the format of your data file. For this demonstration, we’ll use the built-in dataset mtcars
which is a collection of various car attributes. We’ll focus on the mpg
(miles per gallon) column.
Checking Assumptions
Before you perform a one-sample t-test, you must check the following assumptions:
- The data follows a normal distribution.
- The data is randomly sampled.
You can check the normality assumption visually using a histogram or a Q-Q plot, or statistically using tests like the Shapiro-Wilk test. Here’s how to create a Q-Q plot for the mpg
column using the ggplot2
package:
library(ggplot2)
# Create a Q-Q plot
qqnorm(mtcars$mpg)
qqline(mtcars$mpg)

The qqnorm()
function creates a Q-Q plot, and the qqline()
function adds a reference line to the plot. If the points on the Q-Q plot lie approximately along this line, this suggests that the data follows a normal distribution.
Performing the One-Sample T-Test
After confirming that your data meets the assumptions of the test, you can perform the one-sample t-test. In R, this is done using the t.test()
function.
The t.test()
function has the following syntax:
t.test(x, mu = 0, alternative = "two.sided", conf.level = 0.95)
x
: a numeric vector of data values.mu
: the value of the mean under the null hypothesis.alternative
: the alternative hypothesis. It must be one of"two.sided"
,"greater"
or"less"
.conf.level
: the confidence level of the test.
For the mtcars
dataset, let’s say we want to test if the mean mpg
is 20. We can perform a one-sample t-test as follows:
# Perform the one-sample t-test
t.test(mtcars$mpg, mu = 20)
Interpreting the Results
After performing the test, R will output the results which include the t-value, degrees of freedom, p-value, confidence interval, and the mean of the sample.
Here’s an example of what the output might look like:
One Sample t-test
data: mtcars$mpg
t = -0.9098, df = 31, p-value = 0.3702
alternative hypothesis: true mean is not equal to 20
95 percent confidence interval:
18.17763 21.73614
sample estimates:
mean of x
19.9569
Here’s how to interpret this output:
t
: The t-value is the calculated difference represented in units of standard error. The greater the magnitude of T (either positive or negative), the greater the evidence against the null hypothesis. In this case, t is -0.9098.df
: This is the degrees of freedom, which is the number of independent pieces of information that went into calculating the estimate. In this case, df is 31.p-value
: The p-value is the probability of obtaining results as extreme as the observed results of a statistical hypothesis test, assuming that the null hypothesis is correct. A small p-value (typically ≤ 0.05) indicates strong evidence against the null hypothesis. In this case, the p-value is 0.3702, which is greater than 0.05, so we do not reject the null hypothesis.alternative hypothesis
: This is the alternative hypothesis you specified (or the default). In this case, the alternative hypothesis was that the true mean is not equal to 20.95 percent confidence interval
: This is a range of values, derived from the sample, that is likely to contain the population mean. In this case, the 95% confidence interval is between 18.18 and 21.74.mean of x
: This is the sample mean, which in this case is approximately 19.96.
In conclusion, we do not reject the null hypothesis that the true mean mpg of cars in the mtcars
dataset is 20.
Conclusion
A one-sample t-test is a powerful tool to compare a sample mean with a known value. This guide demonstrated how to perform a one-sample t-test in R, from checking the assumptions of the test to interpreting the results. Remember, the interpretation of statistical results is as important as the test itself. It’s critical to use the test that correctly matches your study design, and to be aware of the limitations of the test.