t test

What is t test

t test is used to compare either the means of two sets of data or to compare an observed mean m to a theoretical value mu. There are different types of t test :

  • The one-sample t test used to compare an observed mean with a theoretical mean.
  • The independent t test (or two sample t test) used to compare the means of two unrelated groups of samples.
  • The paired t test used to compare two sets of paired samples.

The different t test formula are described in detail by following this link: t test formula.

The objective of this chapter is to show you how to calculate t test in R.

Note that an online t test calculator is available here to calculate Student’s t-test without any installation.

t.test : R function to calculate t test

The R function to use for t test statistics is t.test(). It can be used to calculate the different types of Student t test mentioned above.

A simplified format of the function is:

# One sample t test :
# Comparison of an observed mean with a
# a theoretical mean
t.test(x, mu=0)
# Independent t test
# Comparison of the means of two independent samples (x & y)
t.test(x, y)
# Paired t test
t.test(x, y, paired=TRUE)

x and y are two numeric vectors.

The argument “alternative =” can be used to specify the alternative hypothesis. It must be one of “two.side” (2 tailed t test, default), “greater” or “less” (one sided t test) (see the R code below ).

t.test(x, y, alternative=c("two.sided", "less", "greater"))

Results of t test R function

The result of t.test() function is a list of class “htest” including the following components :

  • statistic : the value of the t test statistics
  • parameter : the degrees of freedom for the t test statistics
  • p.value : the p-value for the test
  • conf.int : a confidence interval for the mean appropriate to the specified alternative hypothesis.
  • estimate : the means of the two groups being compared (in the case of independent t test) or difference in means (in the case of paired t test).

The format of the R code to use to get these values is as follow:

res$p.value # get the p-value
res$parameter # get the degrees of freedom
res$statistic # get the t test statistics

Many articles are included in this chapter. Scroll down to the bottom of this page to see some examples on how to perform t test in R.


This analysis has been done with R (ver. 3.1.0).

Enjoyed this article? I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Twitter, Facebook or Linked In.

Show me some love with the like buttons below... Thank you and please don't forget to share and comment below!!
Avez vous aimé cet article? Je vous serais très reconnaissant si vous aidiez à sa diffusion en l'envoyant par courriel à un ami ou en le partageant sur Twitter, Facebook ou Linked In.

Montrez-moi un peu d'amour avec les like ci-dessous ... Merci et n'oubliez pas, s'il vous plaît, de partager et de commenter ci-dessous!

This page has been seen 39106 times