How to Perform a Two Sample T-Test in R

Spread the love

The two-sample t-test (or independent t-test) is a statistical procedure used to determine whether the mean difference between two sets of observations is zero. It is used when you want to compare the means of two independent groups to determine if they are different from each other. The null hypothesis of the t-test is that the population means of the two groups are equal.

This article will guide you through the steps to perform a two-sample t-test in R, including data preparation, checking assumptions, running the test, and interpreting the results.

Preparing Your Dataset

To perform a two-sample t-test, your data needs to be in a format where one column represents the groups, and another column represents the observations. In this article, we’ll use the built-in dataset mtcars as an example. We’ll compare the mpg (miles per gallon) between automatic and manual transmission cars (represented by the am column, where 0 = automatic and 1 = manual).

Checking Assumptions

Before performing a two-sample t-test, you need to check the following assumptions:

  1. The dependent variable (mpg in our case) should be measured on a continuous scale.
  2. The two groups are independent of each other.
  3. The dependent variable should follow a normal distribution.
  4. The variances of the dependent variable for the two groups should be equal. This is known as the assumption of homogeneity of variances.

The first two assumptions can usually be checked by understanding your dataset and the method of data collection.

You can check for normality visually using a histogram or a Q-Q plot, or statistically using tests like the Shapiro-Wilk test. Here is how you can create histograms for the two groups using ggplot2 package:


# Histogram for automatic cars
ggplot(mtcars[mtcars$am == 0, ], aes(x = mpg)) +
  geom_histogram(binwidth = 1, color = "black", fill = "white") +
  ggtitle("Histogram for automatic cars")

# Histogram for manual cars
ggplot(mtcars[mtcars$am == 1, ], aes(x = mpg)) +
  geom_histogram(binwidth = 1, color = "black", fill = "white") +
  ggtitle("Histogram for manual cars")

The homogeneity of variances can be checked using Levene’s test, which can be performed using the car package in R:


# Convert 'am' to a factor
mtcars$am <- as.factor(mtcars$am)

# Perform Levene's test
leveneTest(mpg ~ am, data = mtcars)

If the p-value is greater than 0.05, you can assume equal variances, otherwise, you’ll have to adjust your t-test for unequal variances. If unequal variances are detected, use var.equal = FALSE in the t.test() function to perform a Welch’s t-test which does not assume equal variances.

Performing the Two-Sample T-Test

After checking the assumptions, you can perform the two-sample t-test using the t.test() function in R. The syntax for performing a two-sample t-test is as follows:

t.test(x, y, alternative = "two.sided", var.equal = TRUE)
  • x, y: Numeric vectors representing the two groups.
  • alternative: The alternative hypothesis. It can be "two.sided", "greater", or "less".
  • var.equal: A logical variable indicating whether to treat the two variances as being equal. If TRUE, then a Student’s t-test is performed. If FALSE, then a Welch t-test is performed.

The following code can be used to perform a two-sample t-test to compare the mpg of automatic and manual cars:

# Perform the two-sample t-test
t.test(mpg ~ am, data = mtcars, alternative = "two.sided", var.equal = TRUE)

Interpreting the Results

After running the t-test, R will provide an output with the t-value, degrees of freedom, p-value, confidence interval, and the sample means. Here’s an example of what the output might look like:

Two Sample t-test

data:  mpg by am
t = -3.7671, df = 30, p-value = 0.0006
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -11.280194  -3.209684
sample estimates:
mean in group 0 mean in group 1 
       17.14737        24.39231 

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 -3.7671.
  • 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 30.
  • 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.0006, which is less than 0.05, so we 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 difference in means is not equal to 0.
  • 95 percent confidence interval: This is a range of values, derived from the sample, that is likely to contain the population mean difference. In this case, the 95% confidence interval is between -11.28 and -3.21.
  • sample estimates: These are the sample means of the two groups. In this case, the mean mpg for automatic cars (am = 0) is 17.15, and the mean mpg for manual cars (am = 1) is 24.39.

In conclusion, we reject the null hypothesis that the mean mpg is the same for automatic and manual cars. We conclude that there is a significant difference in the mpg between automatic and manual cars.


The two-sample t-test is a powerful tool to compare the means of two independent groups. This article provides a step-by-step guide on how to perform a two-sample t-test in R, from checking the assumptions of the test to interpreting the results. Always remember that the results of a t-test, like any statistical test, are inferential and should be interpreted within the context of your research question and study design.

Posted in RTagged

Leave a Reply