0
0
R Programmingprogramming~5 mins

t-test in R Programming

Choose your learning style9 modes available
Introduction

A t-test helps you check if two groups have different average values. It tells you if the difference is real or just by chance.

Comparing average test scores of two classes to see if one did better.
Checking if a new medicine changes blood pressure compared to no treatment.
Seeing if two machines produce parts with different average sizes.
Testing if a diet plan changes average weight before and after.
Syntax
R Programming
t.test(x, y = NULL, alternative = c("two.sided", "less", "greater"), paired = FALSE, var.equal = FALSE, mu = 0)

x is the first group of numbers.

y is the second group (optional for one-sample test).

Examples
Compare two groups to see if their averages differ.
R Programming
t.test(group1, group2)
Compare measurements before and after on the same subjects.
R Programming
t.test(before, after, paired = TRUE)
Check if the average of x is different from 10.
R Programming
t.test(x, mu = 10)
Sample Program

This program compares two groups of numbers and prints the p-value. The p-value shows if the difference in averages is likely real.

R Programming
group1 <- c(5, 6, 7, 8, 9)
group2 <- c(7, 8, 9, 10, 11)
result <- t.test(group1, group2)
print(result$p.value)
OutputSuccess
Important Notes

A small p-value (usually less than 0.05) means the groups are likely different.

Use paired = TRUE when the same subjects are measured twice.

Set var.equal = TRUE if you believe both groups have the same variance.

Summary

A t-test checks if two groups have different averages.

It gives a p-value to help decide if the difference is real.

Use it for comparing groups or before/after measurements.