T-Test in R Programming: One Sample & Paired Example

โšก Smart Summary

T-Test in R compares means using the t.test() function, covering one sample against a fixed target, two independent groups, and paired repeated measures. This walkthrough runs each variant, reads the p-value correctly, and verifies the underlying assumptions.

  • ๐Ÿ“ Core Statistic: The t-value divides the observed mean difference by its standard error, so larger magnitudes argue against the null hypothesis.
  • ๐Ÿงช One-Sample Test: t.test(x, mu = 10) checks a single vector against a theoretical value such as a recipe specification.
  • ๐Ÿ‘ฅ Two-Sample Test: t.test(x, y) compares two independent groups and defaults to the Welch correction for unequal variance.
  • ๐Ÿ”— Paired Test: Adding paired = TRUE tests the mean of the within-subject differences and removes shared variation.
  • โœ… Assumption Checks: Use shapiro.test() for normality and var.test() for equal variance before reporting any p-value.
  • ๐Ÿ“Š Decision Rule: A p-value below 0.05 rejects the null hypothesis, but never proves the alternative hypothesis true.

T Test in R One Sample Paired

What is Statistical Inference?

Statistical Inference is the art of generating conclusions about the distribution of the data. A data scientist is often faced with questions that can only be answered scientifically. Therefore, statistical inference is a strategy to test whether a hypothesis is true, i.e. validated by the data.

A common strategy for assessing a hypothesis is the t-test, which tells you whether two means are equal. It is also known as the Student test. A t-test can be computed for:

  1. A single vector against a fixed value (one-sample t-test)
  2. Two vectors from two separate groups (independent two-sample t-test)
  3. Two vectors measured on the same subjects (paired t-test)

Every t-test assumes the data are randomly sampled and that the values (or, for a paired test, the differences) come from an approximately normally distributed population. The independent two-sample version additionally assumes the two groups are independent of each other, and the classic form assumes their variances are equal.

What is T-Test in R Programming?

The basic idea behind a T-Test is to use statistics to evaluate two contrary hypotheses:

  • H0: the null hypothesis, that the population mean equals the value being tested
  • H1: the alternative hypothesis, that the population mean differs from that value

The t-test is designed for small sample sizes, where the normal approximation is unreliable. It requires the data to be approximately normally distributed.

T-Test Syntax in R

The basic syntax for t.test() in R is:

t.test(x, y = NULL,
       mu = 0, var.equal = FALSE)
arguments:
- x : A vector to compute the one-sample t-test
- y: A second vector to compute the two sample t-test
- mu: Mean of the population under the null hypothesis
- var.equal: Specify whether the variances of the two vectors are equal. By default, set to `FALSE`
- paired: Set to `TRUE` when the two vectors are repeated measures on the same subjects

Before running anything, match your data layout to the right variant of the test.

Types of T-Test in R

Choosing the wrong variant is the most common t-test mistake, so start by matching your data layout to the right call.

Type Use it when R call
One-sample One group compared with a known target value t.test(x, mu = value)
Independent two-sample (Welch) Two separate groups, variances possibly unequal t.test(x, y)
Independent two-sample (pooled) Two separate groups with equal variance t.test(x, y, var.equal = TRUE)
Paired The same subjects measured twice t.test(x, y, paired = TRUE)
One-sided You only care about a difference in one direction t.test(x, mu = value, alternative = “greater”)

Beyond three groups, a t-test is no longer appropriate. Switch to the ANOVA test, which keeps the overall error rate at 5 percent instead of inflating it across repeated pairwise comparisons.

One Sample T-Test in R

The One Sample t-test, or student’s test, compares the mean of a vector against a theoretical mean, One Sample T-Test in R. The formula used to compute the t-test is:

One Sample T-Test in R

Here,

  • One Sample T-Test in R refers to the mean
  • One Sample T-Test in R to the theoretical mean
  • s is the standard deviation
  • n the number of observations.

To evaluate the statistical significance of the t-test, you need to compute the p-value. The p-value ranges from 0 to 1, and is interpreted as follow:

  • A p-value lower than 0.05 means you can reject the null hypothesis. Note that rejecting H0 is not the same as proving H1 true, it only means the data are unlikely under H0.
  • A p-value higher than 0.05 indicates that you do not have enough evidence to reject the null hypothesis.

You can construct the p-value by looking at the corresponding absolute value of the t-test in the Student distribution with a degrees of freedom equals to One Sample T-Test in R

For instance, with 5 observations you compare your t-value against the Student distribution with 4 degrees of freedom at a 95 percent confidence level. To reject the null hypothesis in a two-sided test, the absolute t-value must exceed 2.776.

Cf table below:

One Sample T-Test in R

One Sample T-Test Example in R

Suppose you are a company producing cookies. Each cookie is supposed to contain 10 grams of sugar. The cookies are produced by a machine that adds the sugar in a bowl before mixing everything. You believe the machine does not add 10 grams of sugar for each cookie. If your assumption is true, the machine needs to be fixed. You stored the level of sugar of thirty cookies.

Note: You can create a randomized vector with the function rnorm(). This function generates normally distributed values. The basic syntax is:

rnorm(n, mean, sd)
arguments
- n: Number of observations to generate
- mean: The mean of the distribution. Optional
- sd: The standard deviation of the distribution. Optional

You can create a distribution with 30 observations with a mean of 9.99 and a standard deviation of 0.04.

set.seed(123)
sugar_cookie <- rnorm(30, mean = 9.99, sd = 0.04)
head(sugar_cookie)

Output:

## [1]  9.967581  9.980793 10.052348  9.992820  9.995172 10.058603

You can use a one-sample t-test to check whether the level of sugar is different than the recipe. You can draw a hypothesis test:

  • H0: The average level of sugar is equal to 10
  • H1: The average level of sugar is different than 10

You use a significance level of 0.05.

# H0 : mu = 10
t.test(sugar_cookie, mu = 10)

Here is the output:

One Sample T-Test Example in R

The p-value of the one-sample t-test is 0.1079, above the 0.05 threshold. The 95 percent confidence interval for the mean runs from 9.973 to 10.002 grams, and it contains the target value of 10. You therefore cannot reject H0: there is not enough evidence that the machine deviates from the recipe.

Independent Two-Sample T-Test in R

The independent two-sample t-test is the most frequently used variant, and it applies whenever the two sets of measurements come from different subjects: two shops, two machines, two treatment arms.

Suppose a factory runs two production lines and you want to know whether they fill jars to the same weight.

set.seed(123)
line_a <- rnorm(25, mean = 500, sd = 8)
line_b <- rnorm(25, mean = 505, sd = 8)

# Welch test, the safe default
t.test(line_a, line_b)

# Pooled test, only when the variances are equal
t.test(line_a, line_b, var.equal = TRUE)

Read the output in four steps.

  1. t is the standardised size of the gap between the two means. Its sign only reflects the order in which you passed the vectors.
  2. df is the degrees of freedom. Welch produces a fractional value; the pooled test gives a whole number equal to n1 + n2 – 2.
  3. p-value is the probability of seeing a gap this large if the true means were identical.
  4. Confidence interval bounds the true difference. When it contains zero, the difference is not significant at that level.

If your data sit in a single data frame with one column of values and one factor column, use the formula interface instead, which is easier to read and avoids splitting the data by hand:

t.test(weight ~ line, data = jars)

Which version to use. Leave var.equal at its default of FALSE unless you have tested and confirmed equal variances. Welch’s correction costs almost nothing in power when the variances happen to match, and it protects you when they do not.

Paired T-Test in R

The paired t-test, also called the dependent sample t-test, applies when the same group is measured twice. Typical applications are:

  • A/B Testing: Compare two variants
  • Case control studies: before and after a treatment on the same subjects

Paired T-Test Example in R

A beverage company is interested in knowing the performance of a discount program on the sales. The company decided to follow the daily sales of one of its shops where the program is being promoted. At the end of the program, the company wants to know if there is a statistical difference between the average sales of the shop before and after the program.

  • The company tracked the sales everyday before the program started. This is our first vector.
  • The program is promoted for one week and the sales are recorded every day. This is our second vector.
  • You will perform the t-test to judge the effectiveness of the program. This is called a paired t-test because the values of both vectors come from the same distribution (i.e., the same shop).

The hypothesis testing is:

  • H0: No difference in mean
  • H1: The two means are different

Remember that the classic t-test assumes an unknown but equal variance in both groups. Real data rarely satisfy that exactly, and ignoring the difference can distort the result.

The remedy is Welch’s t-test, which relaxes the equal-variance assumption. R applies it by default because var.equal is FALSE unless you say otherwise. In this dataset both vectors were generated with the same standard deviation, so you can safely set var.equal = TRUE.

You create two random vectors from a Gaussian distribution with a higher mean for the sales after the program.

set.seed(123)
# sales before the program
sales_before <- rnorm(7, mean = 50000, sd = 50)
# sales after the program.This has higher mean
sales_after <- rnorm(7, mean = 50075, sd = 50)
# draw the distribution
t.test(sales_before, sales_after,var.equal = TRUE)

Paired T-Test Example in R

The p-value is 0.04606, just below the 0.05 threshold, so you reject H0 and conclude the two averages differ significantly. The discount programme appears to have lifted sales.

โš ๏ธ Important: the call above compares the two vectors as independent samples. Because both series are measurements on the same shop, the statistically correct call adds paired = TRUE:

t.test(sales_before, sales_after, paired = TRUE)

The paired form tests the mean of the day-by-day differences instead of the difference of two means. It removes the shop-level variation shared by both vectors and therefore has more statistical power.

How to Check T-Test Assumptions in R

A t-test p-value is only meaningful when its assumptions hold. Each one has a direct check.

1. Normality. The one-sample and two-sample tests assume the values are approximately normal; the paired test assumes the differences are. Inspect a Q-Q plot and confirm with the Shapiro-Wilk test:

qqnorm(sugar_cookie); qqline(sugar_cookie)
shapiro.test(sugar_cookie)

# for a paired design, test the differences
shapiro.test(sales_after - sales_before)

A Shapiro-Wilk p-value above 0.05 means normality cannot be rejected. With more than about 30 observations per group, the central limit theorem makes the t-test robust to moderate skew anyway.

2. Equal variance. Only the pooled two-sample test needs this. Test it with an F-test:

var.test(line_a, line_b)

A p-value above 0.05 supports equal variances, which justifies var.equal = TRUE.

3. Independence. This follows from the study design and cannot be tested after the fact. If the same subject contributes to both vectors, the independent test is simply the wrong tool and you need paired = TRUE.

When an assumption fails. For clearly non-normal data with small samples, use the rank-based alternatives: wilcox.test(x, y) replaces the two-sample t-test and wilcox.test(x, y, paired = TRUE) replaces the paired version. Neither requires normality, though both trade a little power when the data are in fact normal.

T-Test in R: Key Takeaways and Test Reference

  • Statistical Inference is the art of generating conclusions about the distribution of the data.
  • The T-Test belongs to the family of inferential statistics. It is commonly employed to find out if there is a statistical difference between the means of two groups.
  • The one-sample t-test, or Student’s test, compares the mean of a vector against a theoretical mean.
  • The paired t-test, or dependent sample t-test, applies when the same group is measured twice.

The table below summarises each test covered above:

Test Hypothesis to test p-value Code Optional argument
one-sample t-test Mean of a vector is different from the theoretical mean 0.05
t.test(x, mu = mean)
paired sample t-test Mean A is different from mean B for the same subjects 0.05
t.test(A, B, paired = TRUE)
var.equal = TRUE

If you are willing to assume equal variances in an independent two-sample test, set var.equal = TRUE. Leave it at the default FALSE to run the safer Welch correction.

FAQs

A paired test analyses the within-subject differences when the same subjects are measured twice. An independent test compares two separate groups. Using the independent test on paired data discards information and reduces statistical power.

Use Welch whenever you have not confirmed equal variances, which is why R applies it by default. It costs almost no power when variances match and protects the error rate when they do not.

For small samples, switch to the Wilcoxon rank-sum or signed-rank test with wilcox.test(). For larger samples the central limit theorem keeps the t-test reliable despite moderate departures from normality.

Teams use paired t-tests to compare two models across identical cross-validation folds and to judge whether an A/B experiment result is real. The paired design removes fold-to-fold variation from the comparison.

Yes. AI assistants can explain degrees of freedom, translate confidence intervals into plain language, and warn when the wrong variant was chosen. Confirm every reading with your own shapiro.test() and var.test() results.

Summarize this post with: