t_test: t Tests

View source: R/t_test.R

t_testR Documentation

t Tests


Perform a one-sample t test, a paired-sample t test or an independent-samples t test on variables of a data set. The output is printed as a LaTeX table that mimics the look of SPSS output.


t_test(data, variables, group = NULL, mu = 0, conf.level = 0.95)

## S3 method for class 't_test_SPSS'
  statistics = c("test", "statistics"),
  version = r2spss_options$get("version"),
  digits = 3,

## S3 method for class 't_test_SPSS'
  statistics = c("statistics", "test"),
  version = r2spss_options$get("version"),
  digits = 3,

tTest(data, variables, group = NULL, mu = 0, conf.level = 0.95)



a data frame containing the variables.


a character vector specifying numeric variable(s) to be used for testing the mean(s). If group is NULL, a one-sample t test is performed if only one variable is specified, and a paired-sample t test is performed if two variables are specified. If a grouping variable is specified in group, an independent-samples t-test is performed and this should be a character string specifying the numeric variable of interest.


a character string specifying a grouping variable for an independent-samples t-test, or NULL.


a number indicating the true value of the mean for a one-sample t test.


a number between 0 and 1 giving the confidence level of the confidence interval.

object, x

an object of class "t_test_SPSS" as returned by function t_test.


a character string or vector specifying which SPSS tables to produce. Available options are "statistics" for descriptive statistics and "test" for test results. For the to_SPSS method, only one option is allowed (the default is the table of test results), but the print method allows several options (the default is to print all tables).


a character string specifying whether the table should mimic the content and look of recent SPSS versions ("modern") or older versions (<24; "legacy"). The main differences in terms of content are that recent SPSS versions show a one-sided p-value in addition to the two-sided p-value, and that small p-values are displayed differently. For the paired-sample test, recent versions of SPSS also display a label Pair 1 for the selected pair of variables.


an integer giving the number of digits after the comma to be printed in the SPSS tables.


additional arguments to be passed down to format_SPSS.


The print method first calls the to_SPSS method followed by to_latex. Further customization can be done by calling those two functions separately, and modifying the object returned by to_SPSS.


An object of class "t_test_SPSS" with the following components:


a data frame containing the relevant descriptive statistics.


an object of class "htest" as returned by t.test (only one-sample and paired-sample tests).


a character vector containing the name(s) of the relevant numeric variable(s).


an integer giving the number of observations (only paired-sample test).


an object as returned by leveneTest (only independent-samples test).


an object of class "htest" as returned by t.test assuming equal variances (only independent-samples test).


an object of class "htest" as returned by t.test not assuming equal variance (only independent-samples test).


a character string containing the name of the grouping variable (only independent-samples test).


a character string giving the type of t test performed ("one-sample", "paired", or "independent").

The to_SPSS method returns an object of class "SPSS_table" which contains all relevant information in the required format to produce the LaTeX table. See to_latex for possible components and how to further customize the LaTeX table based on the returned object.

The print method produces a LaTeX table that mimics the look of SPSS output.


LaTeX tables that mimic recent versions of SPSS (version = "modern") may require several LaTeX compilations to be displayed correctly.


Andreas Alfons


## one-sample and paired-sample t test

# load data

# test whether the average grade on the resit
# differs from 5.5 (minimum passing grade)
t_test(Exams, "Resit", mu = 5.5)

# test whether average grades differ between the
# regular exam and the resit
t_test(Exams, c("Resit", "Regular"))

## independent-samples t test

# load data
# log-transform market values
Eredivisie$logMarketValue <- log(Eredivisie$MarketValue)

# test whether average log market values differ between
# Dutch and foreign players
t_test(Eredivisie, "logMarketValue", group = "Foreign")

r2spss documentation built on May 25, 2022, 5:05 p.m.