Get startedGet started for free

Comparing two means (5)

You may have been wondering if there is an easier way to do this in R? If so, you are right. In R we can easily use the student t test which actually does a lot of work for us.

The function t.test() performs a student t test. It either takes a vector x which contains data from your first sample and a vector y which contains data from your second sample or you can use the formula interfacet. By using the formula interface you firstly provide your dependent variable then a tilde and lastly your independent variable. An example of this using the built-in sleep dataset is the following: t.test(extra ~ group, data = sleep, conf.level = 0.95). In the example I also provided the data argument as both the extra and the group vectors are contained within the sleep dataframe. Lastly, I provided it with the conf.level argument to specify the confidence level that I want to use. You can always look at the other (optional) arguments of the t test by inspecting the help tabs: help(t.test)

This exercise is part of the course

Inferential Statistics

View Course

Exercise instructions

  • In this exercise we have simulated some data that available in your console as sport. This dataframe contains the hours variable which contains the average number of hours of sport that an individual does per week and the gender variable which contains the gender of the person (for simplicity's sake this only contains the male and the female gender).
  • Perform a student t test on the data by using the formula interface where hours is your dependent variable and gender is you independent variable. Set the argument conf.level to be equal to 0.99. Print the output to the console

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# peform a student t test and print the output to the console
Edit and Run Code