How to Perform a Two Sample T-Test in R

A two sample t-test is used to test whether or not the means of two populations are equal.

You can use the following basic syntax to perform a two sample t-test in R:

t.test(group1, group2, var.equal=TRUE) 

Note: By specifying var.equal=TRUE, we tell R to assume that the variances are equal between the two samples.

If you don’t want to make this assumption, simply leave out this argument and R will instead perform Welch’s t-test, which does not assume that the variances are equal between the samples.

The following example shows how to perform a two sample t-test in R in practice.

Example: Two Sample T-Test in R

Suppose we want to know if two different species of plants have the same mean height.

To test this, we collect a simple random sample of 12 plants from each species.

The following code shows how to perform a two sample t-test in R to determine if the mean height is equal between the two species:

#create vectors to hold plant heights from each sample
group1 <- c(8, 8, 9, 9, 9, 11, 12, 13, 13, 14, 15, 19)
group2 <- c(11, 12, 13, 13, 14, 14, 14, 15, 16, 18, 18, 19)

#perform two sample t-test
t.test(group1, group2, var.equal=TRUE)

	Two Sample t-test

data:  group1 and group2
t = -2.5505, df = 22, p-value = 0.01823
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -5.5904820 -0.5761847
sample estimates:
mean of x mean of y 
 11.66667  14.75000 

Here’s how to interpret the results of the test:

data: The names of the vectors that contain the sample data.

t: The t test-statistic. In this case, it is -2.5505.

df: The degrees of freedom, calculated as n1 + n2 – 2 = 12 + 12 – 2 = 22.

p-value: The p-value that corresponds to a t test-statistic of -2.5505 and df = 22. The p-value turns out to be .01823. We can confirm this value by using the T Score to P Value calculator.

95 percent confidence interval: The 95% confidence interval for the true difference in means between the two groups. It turns out to be [-5.59, -.576].

sample estimates: The sample mean of each group. In this case, the sample mean of group 1 was 11.667 and the sample mean of group 2 was 14.75.

The null and alternative hypotheses for this particular two sample t-test are as follows:

H0: µ1 = µ2 (the two population means are equal)

HA: µ1 ≠µ2 (the two population means are not equal)

Because the p-value of our test (.01823) is less than 0.05, we reject the null hypothesis.

This means we have sufficient evidence to conclude that the mean plant height between the two species is not equal.

Technical Notes

The t.test() function in R uses the following syntax:

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


  • x, y: The names of the two vectors that contain the data.
  • alternative: The alternative hypothesis. Options include “two.sided”, “less”, or “greater.”
  • mu: The value assumed to be the true difference in means.
  • paired: Whether or not to use a paired t-test.
  • var.equal: Whether or not the variances are equal between the two groups.
  • conf.level: The confidence level to use for the test.

Feel free to change any of these arguments when you conduct your own t-test, depending on the particular test you want to perform.

Additional Resources

The following tutorials explain how to perform other common tasks in R:

How to Perform a One Sample T-Test in R
How to Perform Welch’s T-Test in R
How to Perform a Paired Samples T-Test in R

Leave a Reply

Your email address will not be published. Required fields are marked *