How do you perform a one sample t-test in R?

How do you perform a one sample t-test in R?

A one sample t-test in R is a statistical test used to determine whether a sample mean is significantly different from a known population mean. To perform this test, the first step is to import the data into R and assign it to a variable. Then, the t.test() function can be used, specifying the variable and the known population mean as arguments. The output of this function includes the t-statistic, degree of freedom, and p-value, which can be used to determine the significance of the results. Additionally, a confidence interval can be calculated to estimate the range of the true population mean. This test is commonly used in research and data analysis to assess the significance of a sample mean compared to a known population mean.

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 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 
 14.33333 

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)

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.

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

Cite this article

stats writer (2024). How do you perform a one sample t-test in R?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-do-you-perform-a-one-sample-t-test-in-r/

stats writer. "How do you perform a one sample t-test in R?." PSYCHOLOGICAL SCALES, 27 Jun. 2024, https://scales.arabpsychology.com/stats/how-do-you-perform-a-one-sample-t-test-in-r/.

stats writer. "How do you perform a one sample t-test in R?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-do-you-perform-a-one-sample-t-test-in-r/.

stats writer (2024) 'How do you perform a one sample t-test in R?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-do-you-perform-a-one-sample-t-test-in-r/.

[1] stats writer, "How do you perform a one sample t-test in R?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How do you perform a one sample t-test in R?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top