R t.test() Function - t-Test
The R t.test() function is used to perform a t-test, comparing whether the means of two groups differ significantly.
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:
-
x The first data vector.
-
y Optional, the second data vector (for an independent two-sample t-test).
-
alternative Direction of the alternative hypothesis: "two.sided" (two-tailed), "less", "greater".
-
mu The mean under the null hypothesis, defaults to 0.
-
paired Whether to perform a paired t-test.
-
conf.level The confidence level, defaults to 0.95.
Example
Independent two-sample t-test: comparing 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 the t-test
result
<-
t.test
(
method_a, method_b
)
(
result
)
The output of the code above 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 result, the p-value is far less than 0.05, indicating a significant difference in scores between the two teaching methods.
R Language Examples