One Sample T-test in R

A one-sample t-test is used to compare the mean of a sample to a known value or a hypothesized value. In R, you can perform a one-sample t-test using the t.test() function.

Here’s a step-by-step example of how to perform a one-sample t-test in R:

1. First, create your sample data or load it from a dataset.

 


# Example sample data
sample_data <- c(12, 14, 15, 17, 19, 20, 22, 25, 28, 30)

2. Define the hypothesized mean you want to test against. For example, let’s test if the mean of our sample data is significantly different from the hypothesized mean of 18.

hypothesized_mean <- 18

3. Perform the one-sample t-test using the t.test() function.

t_test_result <- t.test(sample_data, mu = hypothesized_mean)

4. Review the results.

print(t_test_result)

This will output the t-test results, including the t-value, degrees of freedom, p-value, and confidence interval.

Output

> t_test_result <- t.test(sample_data, mu = hypothesized_mean)
> print(t_test_result)

One Sample t-test

data: sample_data
t = 1.1531, df = 9, p-value = 0.2786
alternative hypothesis: true mean is not equal to 18
95 percent confidence interval:
15.88408 24.51592
sample estimates:
mean of x 
20.2

If the p-value is less than the significance level (typically 0.05), you can reject the null hypothesis and conclude that there is a significant difference between the sample mean and the hypothesized mean. If the p-value is greater than the significance level, you cannot reject the null hypothesis, and there is insufficient evidence to support a significant difference.

Example – Apples

Here’s another example of a one-sample t-test in R:

Let’s say you have a sample of weights of 20 apples, and you want to test if the average weight of apples in this sample is significantly different from the known average weight of 150 grams.

# Sample data: weights of 20 apples (in grams)
apple_weights <- c(140, 152, 145, 159, 162, 151, 148, 146, 157, 
161, 143, 137, 154, 167, 149, 144, 156, 150, 165, 163)

# Hypothesized population mean
known_mean <- 150

# Perform the one-sample t-test
t_test_result <- t.test(apple_weights, mu = known_mean)

# Print the results
print(t_test_result)

The output will provide you with the t-value, degrees of freedom (df), and the p-value. To interpret the results, you would typically use a significance level (alpha) of 0.05. If the p-value is less than 0.05, you can reject the null hypothesis and conclude that the sample mean is significantly different from the known population mean. If the p-value is greater than 0.05, you cannot reject the null hypothesis, and there’s not enough evidence to conclude that the sample mean is significantly different from the population mean.

Output

> # Perform the one-sample t-test
> t_test_result <- t.test(apple_weights, mu = known_mean)
> 
> # Print the results
> print(t_test_result)

One Sample t-test

data: apple_weights
t = 1.2697, df = 19, p-value = 0.2195
alternative hypothesis: true mean is not equal to 150
95 percent confidence interval:
148.4112 156.4888
sample estimates:
mean of x 
152.45

Example – Students’ test scores

Suppose you have a sample of 15 students’ test scores, and you want to test if the average test score of this sample is significantly different from the known average test score of 75 points.

# Sample data: test scores of 15 students
student_scores <- c(68, 74, 80, 77, 82, 72, 76, 
70, 88, 81, 66, 78, 75, 79, 84)

# Hypothesized population mean
known_mean <- 75

# Perform the one-sample t-test
t_test_result <- t.test(student_scores, mu = known_mean)

# Print the results
print(t_test_result)

The output will provide you with the t-value, degrees of freedom (df), and the p-value. To interpret the results, you would typically use a significance level (alpha) of 0.05. If the p-value is less than 0.05, you can reject the null hypothesis and conclude that the sample mean is significantly different from the known population mean. If the p-value is greater than 0.05, you cannot reject the null hypothesis, and there’s not enough evidence to conclude that the sample mean is significantly different from the population mean.

Output

> t_test_result <- t.test(student_scores, mu = known_mean)
> 
> # Print the results
> print(t_test_result)

One Sample t-test

data: student_scores
t = 1.066, df = 14, p-value = 0.3045
alternative hypothesis: true mean is not equal to 75
95 percent confidence interval:
73.31335 80.01998
sample estimates:
mean of x 
76.66667

T-Test in R Programming

Two sample T-test in R