Performing a student's t-test
A one sample t-test enables us to test whether two means are significantly different; a two sample t-test allows us to test whether the means of two independent groups are different. Using t-tests, we can find how significant the difference is and if the difference has happened by chance. In this recipe, we will discuss how to conduct one sample t-test and two sample t-tests using R.
Getting ready
Ensure that mtcars has already been loaded into a DataFrame within an R session. As the t.test function originates from the stats package, make sure the library, stats, is loaded.
How to do it...
Perform the following steps:
- First, we visualize the attribute,
mpg, againstamusing a boxplot:
> boxplot(mtcars$mpg, mtcars$mpg[mtcars$am==0], ylab = "mpg",
names=c("overall","automobile"))
> abline(h=mean(mtcars$mpg),lwd=2, col="red")
> abline(h=mean(mtcars$mpg[mtcars$am==0]),lwd=2, col="blue")
The boxplot of mpg of the overall population...