 KoalaTea

# How to use T-test in R to Compare Means

## Introduction

One common test on samples is the mean test. The t-test is widely used in this regard. For example, maybe you know that the average sales for your company per a week is \$500, and you just collected a couple of weeks of sales data. You would like to conduct a hypothesis test as to if your new sample is different than the avg \$500. In this article, we will learn how to use the `t-test` to test the mean of a sample in R.

## The Basic Test

Continuing from our example, let’s say we have collected five weeks of sales data. We know that the average sales per week is \$500, so we will conduct a test to see if our recent samples differ. We can use the `t.test` method to perform this test.

``````recent.sales = c(400, 600, 850, 550)
avg.sales = 500

t.test(recent.sales, mu=avg.sales)``````
``````##
##  One Sample t-test
##
## data:  recent.sales
## t = 1.069, df = 3, p-value = 0.3634
## alternative hypothesis: true mean is not equal to 500
## 95 percent confidence interval:
##  302.3094 897.6906
## sample estimates:
## mean of x
##       600``````
``````# One Sample t-test

# data:  recent.sales
# t = 1.069, df = 3, p-value = 0.3634
# alternative hypothesis: true mean is not equal to 500
# 95 percent confidence interval:
#  302.3094 897.6906
# sample estimates:
# mean of x
#       600 ``````

There is quite a bit of information here, but the focus for now will be on the `p-value`. Since this number is higher than `.05`, in this case we will fail to reject the null hypothesis. That means, we do not have enough evidence to say that our sample data is different from the average.

## Viewing the T-test object

Now, that we have conducted our first t-test. Let’s take a look at the response object to see what other information is returned. We can use the `names` method to see which properties are on the return t-test.

``````result = t.test(recent.sales, mu=avg.sales)

names(result)``````
``````##   "statistic"   "parameter"   "p.value"     "conf.int"    "estimate"
##   "null.value"  "stderr"      "alternative" "method"      "data.name"``````

You can see there are quite a bit of values returned. These are helpful for different reasons. Let’s extract the `p.value` and `conf.int` for an example.

``result\$p.value``
``##  0.3634261``
``result\$conf.int``
``````##  302.3094 897.6906
## attr(,"conf.level")
##  0.95``````