R t.test() Function - t-test

R 语言实例R Language Examples

The R t.test() function is used to perform a t-test to compare whether there is a significant difference between the means of two groups of data.

The t-test is one of the most commonly used hypothesis testing methods in statistical analysis.

The syntax of the t.test() function is as follows:

t.test(x, y = NULL, alternative = "two.sided", mu = 0, paired = FALSE, conf.level = 0.95)

Parameter description:

  • xA vector of data for the first group.

  • yOptional, vector of data for the second group (used for independent two-sample t-test).

  • alternativeDirection of the alternative hypothesis: "two.sided" (two-tailed), "less", "greater".

  • muThe mean in the null hypothesis, default is 0.

  • pairedWhether to perform a paired t-test.

  • conf.levelConfidence level, default is 0.95.

Example

# Independent two-sample t-test: compare the effects of two teaching methods
method_a <- c(78, 82, 85, 88, 90, 75, 80, 86, 92, 84)
method_b <- c(72, 75, 78, 76, 80, 70, 74, 77, 79, 73)

# Perform t-test
result <- t.test(method_a, method_b)
print(result)

The output of executing the above code is:

    Welch Two Sample t-test

data:  method_a and method_b
t = 4.4029, df = 17.764, p-value = 0.0003513
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
  4.640839 13.159161
sample estimates:
mean of x mean of y
     84.0      75.1

From the results, it can be seen that the p-value is much less than 0.05, indicating that there is a significant difference in the scores of the two teaching methods.

R 语言实例R Language Examples

Other Extensions