t-test for independent samples in R

You have two groups—for example, men and women—and want to know whether they differ in a particular characteristic such as perceived stress, learning outcomes, or reaction time. For this, you need the independent-samples t-test. In this article, I’ll show you how to conduct and interpret it in R.


When do you need this test?

Definition:
The independent-samples t-test tests whether the means of two groups differ significantly from one another.
Null hypothesis: $H_0: \mu_1 = \mu_2$
Alternative hypothesis: $H_1: \mu_1 e \mu_2$ (two-tailed)

Requirement: The groups must be independent of one another. In other words, each person belongs to only one group.


Example: Exam stress in two degree programs

Suppose you have data from students in two degree programs: Psychology and Business Administration. You want to know whether their average exam stress differs.

# Create example data
set.seed(42)
group <- rep(c("Psychology", "Business Administration"), each = 30)
stress <- c(rnorm(30, mean = 7, sd = 1), rnorm(30, mean = 6.5, sd = 1.2))
data <- data.frame(group, stress)

Conducting the t-test in R

# Independent-samples t-test
t.test(stress ~ group, data = data)

R automatically detects which variable is the grouping variable (gruppe) and which is the dependent variable (stress). By default, equal variances are not assumed (Welch’s test). You can also override this default setting so that the classical t-test is performed (see below!).


Example output (abridged)

Welch Two Sample t-test

data: stress by gruppe
t = 1.865, df = 56.12, p-value = 0.067
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-0.029 1.109
sample estimates:
mean in group BWL mean in group Psychology
6.460 7.030

Interpretation of the results

MetricMeaning
t = 1.865Test statistic – magnitude of the difference between the means
df = 56.12Degrees of freedom (approximate, since the variances are unequal)
p-value = 0.067The p-value – it is greater than 0.05
Confidence intervalIncludes 0 → no significant difference
MeansPsychology: 7.03, BWL: 6.46

Conclusion: No significant difference at the 5% level. There is a trend, but it is not strong enough to reject the null hypothesis.


Test for equality of variances

You can test for equality of variances with an F-test:

var.test(stress ~ gruppe, data = daten)

If the p-value > 0.05, you can assume equal variances. You can then use the classical t-test:

t.test(stress ~ gruppe, data = daten, var.equal = TRUE)



Summary

StepWhat you do
1Structure the data: metric outcome variable + grouping variable
2Run the test: t.test(y ~ gruppe, data = ...)
3Optional: var.test() for equality of variances
4Interpret the result: p-value, confidence interval, means
5Optional: visualization with boxplot()

Q&A to think about

Question 1: When do you use the t-test for independent samples?
Answer: When you want to compare two independent groups—for example, men vs. women.

Question 2: What is the difference between var.equal = TRUE and FALSE?
Answer: TRUE assumes equal variances (classical t-test), while FALSE (the default) uses the more robust Welch test when variances are unequal.

Question 3: How can you test whether the variances are equal?
Answer: Using the var.test() command — if the p-value is > 0.05, equality can be assumed.

Question 4: What does a p-value of 0.067 mean?
Answer: There is no statistically significant difference at the 5% level, but there may be one at the 10% level (indicating a possible trend).

Alles klar?

Ich hoffe, der Beitrag war für dich soweit verständlich. Wenn du weitere Fragen hast, nutze bitte hier die Möglichkeit, eine Frage an mich zu stellen!

Stelle Dominik eine Frage