Two Independent Sample t-Test using R

 

Use the mpg data file with a new variable drv_front. The new drv_front variable is defined as 1 if drv = ‘f’ or 0 if not equal ‘f’. The var.equal option in t.test function can be used to specify equal variance or not.

> mpg$drv_front <- NA
> mpg[which(mpg$drv == 'f'), ]$drv_front <- 1
> mpg[which(mpg$drv != 'f'), ]$drv_front <- 0
> attach(mpg)
> # Equal Variance
> t.test(hwy ~ drv_front, var.equal = TRUE)

 Two Sample t-test

 data: hwy by drv_front
 t = -15.9362, df = 232, p-value < 2.2e-16
 alternative hypothesis: true difference in means is not equal to 0
 95 percent confidence interval:
 -9.695975 -7.562280
 sample estimates:
 mean in group 0 mean in group 1
 19.53125 28.16038
> # Unequal Variance (default)
> t.test(hwy ~ drv_front)

 Welch Two Sample t-test

 data: hwy by drv_front
 t = -15.8801, df = 220.657, p-value < 2.2e-16
 alternative hypothesis: true difference in means is not equal to 0
 95 percent confidence interval:
 -9.700030 -7.558225
 sample estimates:
 mean in group 0 mean in group 1
 19.53125 28.16038

 

 

See Also:

Do the same test in SAS

 

 

 

 

 

 

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *