TwoSample ttest in R (original) (raw)

Two-Sample t-test in R

Last Updated : 24 Jul, 2025

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

**Syntax:

t.test(x, y, alternative = "two.sided", mu = 0, paired = FALSE, var.equal = FALSE, conf.level = 0.95)

**Parameters:

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.

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_result <- t.test(heights_male, heights_female) print(t_test_result)

`

**Output:

data

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 (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.