The “t.test” Function in R

  • Package: Base R (no specific package required)

  • Purpose: Performs t-tests for comparing means of two groups or one-sample t-tests.

  • General Class: Statistical Testing

  • Required Argument(s):

    • x: A numeric vector of data values for the first group (or the only group in the case of a one-sample t-test).

  • Notable Optional Arguments:

    • y: An optional numeric vector of data values for the second group. Used in two-sample t-tests.

    • alternative: A character string specifying the alternative hypothesis. Options include "two-sided", "greater", or "less".

    • mu: A numeric value specifying the hypothesized population mean in a one-sample t-test.

    • paired: A logical value indicating whether to perform a paired t-test. The default is FALSE.

    • var.equal: A logical value indicating whether to assume equal variances in a two-sample t-test. The default is FALSE.

    • conf.level: The confidence level for the confidence interval. The default is 0.95.

  • Example:

  • # Example data for a two-sample t-test
    set.seed(123)
    group1 <- rnorm(30, mean = 10, sd = 2)
    group2 <- rnorm(30, mean = 12, sd = 2)

    # Perform a two-sample t-test
    result <- t.test(group1, group2)

    # Display the result
    print(result)

  • In this example, the t.test function is used to perform a two-sample t-test comparing the means of two groups (group1 and group2). The result of the test, including the t-statistic, degrees of freedom, and p-value, is then printed to the console.

Previous
Previous

The “lm” Function in R

Next
Next

The “rweibull” Function in R