Learn R Programming

statuser (version 0.1.9)

t.test2: Enhanced alternative to t.test()

Description

The basic t-test function in R, t.test, does not report the observed difference of means, does not stipulate which mean is subtracted from which (i.e., whether it computed A-B or B-A), and presents the test results on the console in a verbose unorganized paragraph of text. t.test2 improves on all those counts, and in addition, it reports the number of observations per group and if any observations are missing it issues a warning. It returns a dataframe instead of a list.

Value

A data frame with class c("t.test2", "data.frame") containing a single row with the following columns:

mean columns

One or two columns containing group means, named after the input variables (e.g., men, women) or Group 1, Group 2 for long names.

diff column

For two-sample tests, the difference between means (e.g., men-women).

ci

The confidence level as a string (e.g., "95 percent").

ci.L, ci.H

Lower and upper bounds of the confidence interval.

t

The t-statistic.

df

Degrees of freedom.

p.value

The p-value.

N columns

Sample sizes, named N(group1), N(group2) or N1, N2. For paired tests, a single N column.

correlation

For paired tests only, the correlation between pairs.

Attributes store additional information including missing value counts and test type (one-sample, two-sample, paired, Welch vs. Student).

Arguments

...

Arguments passed to t.test

Examples

Run this code
# Two-sample t-test
men <- rnorm(100, mean = 5, sd = 1)
women <- rnorm(100, mean = 4.8, sd = 1)
t.test2(men, women)

# Paired t-test
x <- rnorm(50, mean = 5, sd = 1)
y <- rnorm(50, mean = 5.2, sd = 1)
t.test2(x, y, paired = TRUE)

# One-sample t-test
data <- rnorm(100, mean = 5, sd = 1)
t.test2(data, mu = 0)

# Formula syntax
data <- data.frame(y = rnorm(100), group = rep(c("A", "B"), 50))
t.test2(y ~ group, data = data)

Run the code above in your browser using DataLab