Independent t test in R

September 13, 2016

(This article was first published on R-exercises, and kindly contributed to R-bloggers)

t-test-box-plotsThe independent t test is used to test if there is any statistically significant difference between two means. Use of an independent t test requires several assumptions to be satisfied. The assumptions are listed below

  1. The variables are continuous and independent
  2. The variables are normally distributed
  3. The variances in each group are equal

When these assumptions are satisfied the results of the t test are valid. Otherwise they are invalid and you need to use a non-parametric test. When data is not normally distributed you can apply transformations to make it normally distributed.

For this exercise it is important to have a good understanding of data normality and hypothesis testing.

For this set of exercises we will use a motor trend car road tests data set. This data is already available in R as mtcars. The data consists of fuel consumption and vehicle characteristics related to design and the level of performance. Our interest in this exercise is to test if there are any significant differences in miles per gallon achieved between manual and automatic transmission vehicles.

Answers to the exercises are available here. If you have an alternative answer please post in the comments.

Exercise 1

Inspect the structure of the data

Exercise 2

Label the am (0,1) variable into automatic and manual categories

Check data labeling was successful

Exercise 3

Attach mtcars data so that its variables are easily accessible

Exercise 4

Generate descriptive statistics for each group

Exercise 5

Generate box plot for each group

Exercise 6

Test for normality  in each group

Exercise 7

Perform a Levene test for equality of variances in the two groups

Exercise 8

Apply a log transformation to stabilize data variance

Exercise 9

Perform a t test on the transformed variable

Exercise 10

Interpret the results

To leave a comment for the author, please follow the link and comment on their blog: R-exercises. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)