Search code examples
rt-test

Changing order of variable in a t-test


I am working on a task where I have a data frame df.3 where I filtered the data frame Cars93 from library(MASS) to retain only the cars of two kinds of 4 cylinders and 6 cylinders or (type c1 and c2)

I am trying to use t-test to approximate a 90% confidence interval for the difference between the means of the column Min.Price of the two kinds of cylinders

However as default, t-test will make a comparison to take cylinders 4 - 6. Is there a way to have 6 - 4? Thanks for any help in advanced

I have done a t-test as below:

var.test(Min.Price ~ Cylinders, df.3)

t.test(Min.Price ~ Cylinders, df.3, conf=0.9)
t.test(df.3$Min.Price ~ df.3$Cylinders,conf=0.9)$conf.int

Solution

  • Use factor to reorder the Cylinders column as you see fit. Alternatively, you could use the rev function.

    library(MASS)
    
    new_data <- subset(Cars93, Cylinders %in% c("4", "6"))
    new_data$Cylinders_f <- factor(new_data$Cylinders, levels = c("6", "4"))
    t.test(Min.Price ~ Cylinders_f, data = new_data)
    
        Welch Two Sample t-test
    
    data:  Min.Price by Cylinders_f
    t = 6.1322, df = 45.029, p-value = 1.982e-07
    alternative hypothesis: true difference in means between group 6 and group 4 is not equal to 0
    95 percent confidence interval:
      6.112782 12.091958
    sample estimates:
    mean in group 6 mean in group 4 
           21.50645        12.40408