How to Perform a One Sample T-Test in R

A one sample t-test is used to determine whether or not the mean of a population is equal to some value.

You can use the following basic syntax in R to perform a one sample t-test:

t.test(data, mu=10)

The following example shows how to use this syntax in practice.

Example: One Sample T-Test in R

Suppose a botanist wants to know if the mean height of a certain species of plant is equal to 15 inches.

She collects a simple random sample of 12 plants and records each of their heights in inches.

She can use the following code to perform a one sample t-test in R to determine if the mean height for this species of plant is actually equal to 15 inches:

#create vector to hold plant heights
my_data <- c(14, 14, 16, 13, 12, 17, 15, 14, 15, 13, 15, 14)

#perform one sample t-test
t.test(my_data, mu=15)

	One Sample t-test

data:  my_data
t = -1.6848, df = 11, p-value = 0.1201
alternative hypothesis: true mean is not equal to 15
95 percent confidence interval:
 13.46244 15.20423
sample estimates:
mean of x 

Here is how to interpret each value in the output:

data: The name of the vector used in the t-test. In this example, we used my_data.

t: The t test-statistic, calculated as (x – μ) / (s√n) = (14.333-15)/(1.370689/√12) = -1.6848.

df: The degrees of freedom, calculated as n-1 = 12-1 = 11.

p-value: The two-tailed p-value that corresponds to a t test-statistic of -1.6848 and 11 degrees of freedom. In this case, p = 0.1201.

95 percent confidence interval: The 95% confidence for the true population mean, calculated to be [13.46244, 15.20423].

The null and alternative hypotheses for this one sample t-test are as follows:

H0µ = 15 (the mean height for this species of plant is 15 inches)

HAµ ≠15 (the mean height is not 15 inches)

Because the p-value of our test (0.1201) is greater than 0.05, we fail to reject the null hypothesis of the test.

This means we do not have sufficient evidence to say that the mean height for this particular species of plant is different from 15 inches.

Additional Resources

The following tutorials explain how to perform other common tests in R:

How to Perform a Two Sample T-Test in R
How to Perform a Paired Samples T-Test in R
How to Perform Welch’s T-Test in R

Leave a Reply

Your email address will not be published.