The two-sample t-test compares the means of two independent groups. It checks whether the difference between these means is statistically significant. We often use this test when comparing results between two separate groups, like the heights or test scores of students in different classes.
Assumptions of the Two-Sample t-test
- The data in each group is independent.
- The populations are normally distributed.
- The variances of the two groups are equal (unless stated otherwise).
Syntax:
t.test(x, y, alternative = "two.sided", mu = 0, paired = FALSE, var.equal = FALSE, conf.level = 0.95)
Parameters:
- x and y: These are the two numeric vectors or data frames we want to compare.
- alternative: This tells the type of test we are performing. It can be "two.sided", "less", or "greater".
- mu: This is the expected difference in means if the null hypothesis is true. The default is 0.
- paired: We set this to TRUE when comparing paired data like before-and-after results.
- var.equal: We set this to TRUE if we assume both groups have equal variance.
- conf.level: This is the confidence level we want, usually set to 0.95.
Performing the Two-Sample t-test in R
We want to compare the heights of two groups of students, male and female, to see if there's a significant difference in their average heights.
1. Creating the Data
We first create two numeric vectors to represent the data for each group.
- c: Used to combine values into a vector.
- heights_male: Vector storing height values for male students.
- heights_female: Vector storing height values for female students.
heights_male <- c(170, 175, 180, 165, 172)
heights_female <- c(160, 165, 170, 155, 162)
2. Performing the t-test
We use the t.test() function to compare the two groups. This function performs the test and returns results like the p-value and confidence interval.
- t.test: Performs the two-sample t-test.
- print: Displays the output of the test.
t_test_result <- t.test(heights_male, heights_female)
print(t_test_result)
Output:

3. Interpreting the Results
We use the p-value to decide if there is a significant difference. If the p-value is less than 0.05, we say the difference is statistically significant.
- if: Checks a condition.
- p.value: Extracts the p-value from the test result.
if (t_test_result$p.value < 0.05) {
print("There is a significant difference in the average heights of male and female students.")
} else {
print("There is no significant difference in the average heights of male and female students.")
}
Output:
[1] "There is a significant difference in the average heights of male and female students."
The two-sample t-test is a handy way to compare averages between two groups.