t-tests in R

Run one-sample, independent-samples, and paired t-tests in R with t.test(), read the output, and report the result in APA style. Runnable examples included.

About 15 minutes, every example runs in your browser

The t-test asks whether a mean differs from a reference value, or whether two means differ from each other, given the noise in your data. One R function, t.test(), covers all three classic variants: one-sample, independent-samples, and paired.

One-sample t-test

Compare one group's mean against a fixed value. Suppose the national average on a wellbeing scale is 50, and you want to know if your sample differs:

wellbeing <- c(52, 61, 48, 57, 55, 49, 63, 58, 51, 54)

t.test(wellbeing, mu = 50)

Output

	One Sample t-test

data:  wellbeing
t = 3.0453, df = 9, p-value = 0.0139
alternative hypothesis: true mean is not equal to 50
95 percent confidence interval:
 51.23436 58.36564
sample estimates:
mean of x
     54.8

Read it in this order: the estimate (mean 54.8), the confidence interval (51.2 to 58.4), then t, df, and p. Here p = .014, so a mean this far from 50 would be unlikely if the true mean were 50.

Independent-samples t-test

Compare two separate groups using the outcome ~ group formula from the last lesson. In mtcars, does fuel efficiency differ between automatic (am = 0) and manual (am = 1) cars?

t.test(mpg ~ am, data = mtcars)

Output

	Welch Two Sample t-test

data:  mpg by am
t = -3.7671, df = 18.332, p-value = 0.001374
alternative hypothesis: true difference in means between group 0 and group 1 is not equal to 0
95 percent confidence interval:
 -11.280194  -3.209684
sample estimates:
mean in group 0 mean in group 1
       17.14737        24.39231

R runs the Welch version by default, which does not assume equal variances in the two groups. That is the safer default; report it as a Welch t-test.

Paired t-test

When the same participants are measured twice (before/after, condition A/condition B), the pairs matter. R's built-in sleep data measures extra sleep under two drugs for the same 10 patients:

before <- sleep$extra[sleep$group == 1]
after  <- sleep$extra[sleep$group == 2]

t.test(after, before, paired = TRUE)

Output

	Paired t-test

data:  after and before
t = 4.0621, df = 9, p-value = 0.002833
alternative hypothesis: true mean difference is not equal to 0
95 percent confidence interval:
 0.7001142 2.4598858
sample estimates:
mean difference
           1.58

Reporting it in APA style

A t-test result sentence needs the statistic, degrees of freedom, p-value, and an effect size. For the independent-samples example above, the skeleton is:

Manual cars had higher fuel efficiency than automatic cars, t(18.33) = 3.77, p = .001.

  • APA drops the leading zero for p (it cannot exceed 1): write p = .001, not 0.001
  • Round t to two decimals; give exact p unless p < .001
  • Journals also expect an effect size (Cohen's d or Hedges' g); base R does not compute it for you, which is exactly the kind of finishing step KyroStat automates

Two groups compared. But what if you have three or more? That is analysis of variance, next.

Ready to turn your spreadsheet into results?

Create an account and run your first analysis in minutes. No install, no statistics course required.