How to Perform t-test in R: Syntax and Examples
In R, you perform a t-test using the
t.test() function. It compares the means of two groups to see if they are significantly different. You provide the data vectors or a formula and specify options like paired or unpaired tests.Syntax
The basic syntax of the t.test() function is:
t.test(x, y = NULL, alternative = "two.sided", paired = FALSE, var.equal = FALSE, conf.level = 0.95)
Where:
x: numeric vector of data values or a formula.y: optional numeric vector for two-sample tests.alternative: type of test - "two.sided", "less", or "greater".paired: TRUE for paired t-test, FALSE for independent samples.var.equal: TRUE assumes equal variances, FALSE uses Welch's test.conf.level: confidence level for the interval.
r
t.test(x, y = NULL, alternative = "two.sided", paired = FALSE, var.equal = FALSE, conf.level = 0.95)
Example
This example shows how to perform an independent two-sample t-test comparing two groups of data.
r
group1 <- c(5.1, 4.9, 5.0, 5.2, 5.3) group2 <- c(5.8, 6.0, 5.9, 6.1, 6.2) result <- t.test(group1, group2) print(result)
Output
Welch Two Sample t-test
data: group1 and group2
t = -7.0711, df = 7.998, p-value = 0.000105
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-1.370645 -0.829355
sample estimates:
mean of x mean of y
5.1 6.0
Common Pitfalls
Common mistakes when using t.test() include:
- Not specifying
paired = TRUEfor paired data, which leads to wrong results. - Assuming equal variances without setting
var.equal = TRUEwhen appropriate. - Passing non-numeric data or vectors of different lengths.
Always check your data and test assumptions before running the t-test.
r
## Wrong: paired data but paired=FALSE (default) group1 <- c(100, 102, 98, 101, 99) group2 <- c(102, 103, 97, 100, 100) t.test(group1, group2) ## Right: paired=TRUE for paired samples t.test(group1, group2, paired = TRUE)
Output
Welch Two Sample t-test
data: group1 and group2
t = -0.36515, df = 7.998, p-value = 0.724
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-3.927 2.527
sample estimates:
mean of x mean of y
100.0 100.4
Paired t-test
data: group1 and group2
t = -1.8974, df = 4, p-value = 0.134
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-3.370 0.570
sample estimates:
mean of the differences
-1.4
Quick Reference
| Parameter | Description | Default |
|---|---|---|
| x | Numeric vector of data or formula | Required |
| y | Second numeric vector for two-sample test | NULL |
| alternative | "two.sided", "less", or "greater" | "two.sided" |
| paired | Use paired t-test if TRUE | FALSE |
| var.equal | Assume equal variances if TRUE | FALSE |
| conf.level | Confidence level for interval | 0.95 |
Key Takeaways
Use t.test() in R to compare means between two groups.
Set paired=TRUE for paired samples to get correct results.
Use var.equal=TRUE only if you know variances are equal.
Check data types and lengths before running the test.
Interpret p-value to decide if means differ significantly.