Performs one and two sample t-tests.
The mosaic `t.test`

provides wrapper functions around the function
of the same name in stats.
These wrappers provide an extended interface that allows for a more systematic
use of the formula interface.

```
t_test(x, ...)
t.test(x, ...)
# S3 method for formula
t_test(formula, data, ..., groups = NULL)
# S3 method for default
t_test(
x,
y = NULL,
alternative = c("two.sided", "less", "greater"),
mu = 0,
paired = FALSE,
var.equal = FALSE,
conf.level = 0.95,
...
)
```

- x
a (non-empty) numeric vector of data values.

- ...
further arguments to be passed to or from methods.

- formula
a formula of the form

`lhs ~ rhs`

where`lhs`

is a numeric variable giving the data values and`rhs`

either`1`

for a one-sample or paired test or a factor with two levels giving the corresponding groups. If`lhs`

is of class`"Pair"`

and`rhs`

is`1`

, a paired test is done- data
an optional matrix or data frame (or similar: see

`model.frame`

) containing the variables in the formula`formula`

. By default the variables are taken from`environment(formula)`

.- groups
When

`x`

is a formula,`groups`

can be used to compare groups:`x = ~ var, groups = g`

is equivalent to`x = var ~ g`

. See the examples.- y
an optional (non-empty) numeric vector of data values.

- alternative
a character string specifying the alternative hypothesis, must be one of

`"two.sided"`

(default),`"greater"`

or`"less"`

. You can specify just the initial letter.- mu
a number indicating the true value of the mean (or difference in means if you are performing a two sample test).

- paired
a logical indicating whether you want a paired t-test.

- var.equal
a logical variable indicating whether to treat the two variances as being equal. If

`TRUE`

then the pooled variance is used to estimate the variance otherwise the Welch (or Satterthwaite) approximation to the degrees of freedom is used.- conf.level
confidence level of the interval.

an object of class `htest`

This is a wrapper around `stats::t.test()`

from the stats package
to extend the functionality of the formula interface. In particular, one can
now use the formula interface for a 1-sample t-test. Before, the formula interface
was only permitted for a 2-sample test. The type of formula that can be used
for the 2-sample test has also be broadened. See the examples.

```
t.test(HELPrct$age)
#>
#> One Sample t-test
#>
#> data: HELPrct$age
#> t = 98.419, df = 452, p-value < 2.2e-16
#> alternative hypothesis: true mean is not equal to 0
#> 95 percent confidence interval:
#> 34.94150 36.36534
#> sample estimates:
#> mean of x
#> 35.65342
#>
# We can now do this with a formula
t.test(~ age, data = HELPrct)
#>
#> One Sample t-test
#>
#> data: age
#> t = 98.419, df = 452, p-value < 2.2e-16
#> alternative hypothesis: true mean is not equal to 0
#> 95 percent confidence interval:
#> 34.94150 36.36534
#> sample estimates:
#> mean of x
#> 35.65342
#>
# data = can be omitted, but it is better to use it
t.test(~ age, HELPrct)
#>
#> One Sample t-test
#>
#> data: age
#> t = 98.419, df = 452, p-value < 2.2e-16
#> alternative hypothesis: true mean is not equal to 0
#> 95 percent confidence interval:
#> 34.94150 36.36534
#> sample estimates:
#> mean of x
#> 35.65342
#>
# the original 2-sample formula
t.test(age ~ sex, data = HELPrct)
#>
#> Welch Two Sample t-test
#>
#> data: age by sex
#> t = 0.92976, df = 179.74, p-value = 0.3537
#> alternative hypothesis: true difference in means between group female and group male is not equal to 0
#> 95 percent confidence interval:
#> -0.8800365 2.4482932
#> sample estimates:
#> mean in group female mean in group male
#> 36.25234 35.46821
#>
# alternative 2-sample formulas
t.test(~ age | sex, data = HELPrct)
#>
#> Welch Two Sample t-test
#>
#> data: age by sex
#> t = 0.92976, df = 179.74, p-value = 0.3537
#> alternative hypothesis: true difference in means between group female and group male is not equal to 0
#> 95 percent confidence interval:
#> -0.8800365 2.4482932
#> sample estimates:
#> mean in group female mean in group male
#> 36.25234 35.46821
#>
t.test(~ age, groups = sex, data = HELPrct)
#>
#> Welch Two Sample t-test
#>
#> data: age by sex
#> t = 0.92976, df = 179.74, p-value = 0.3537
#> alternative hypothesis: true difference in means between group female and group male is not equal to 0
#> 95 percent confidence interval:
#> -0.8800365 2.4482932
#> sample estimates:
#> mean in group female mean in group male
#> 36.25234 35.46821
#>
# 2-sample t from vectors
with(HELPrct, t.test(age[sex == "male"], age[sex == "female"]))
#>
#> Welch Two Sample t-test
#>
#> data: age[sex == "male"] and age[sex == "female"]
#> t = -0.92976, df = 179.74, p-value = 0.3537
#> alternative hypothesis: true difference in means is not equal to 0
#> 95 percent confidence interval:
#> -2.4482932 0.8800365
#> sample estimates:
#> mean of x mean of y
#> 35.46821 36.25234
#>
# just the means
mean(age ~ sex, data = HELPrct)
#> female male
#> 36.25234 35.46821
```