0

I am working on an assignment in RStudio, examining the difference between car makes and their crash safety ratings. Right now I only want to test b/w Ford and Chevrolet but the "make" category has another 20 makes. To perform a simple T Test using these variables I tried

t.test(Head_IC~make, alternative= "two.sided", paired=T)

Which gave me

"grouping factor must have exactly 2 levels"

I looked around on stack and found people generally use a comma to fix this error. I found that if I place a comma b/w "Head_IC" and "make" I get another, separate error. Is my issue b/w my x and y value? Or is it b/c "make" consists of several different brands? Thanks for the help!

1 Answers1

1

R uses what is called "methods" to conduct different behavior depending on what kind of object is given to a function.

t.test(Head_IC~make, alternative= "two.sided", paired=T)

is different than

t.test(Head_IC, make, alternative= "two.sided", paired=T)

in that the first accepts a formula argument, where all the numeric data are in Head_IC and all of the category data are in make. The second form assumes that all of the numeric data for the first group are in Head_IC and all of the numeric data for the second group are in make.

This is beneficial because it allows you to conveniently get to the same result even though your data may have slightly different formats.

Unfortunately, as you've found, your data are not in a format that is suitable to using t.test. There are a few ways you can approach this.

Subset your data

data_subset <- your_data_object[your_data_object$make %in% c("Ford", "Chevrolet"), ]
t.test(Head_IC~make, alternative= "two.sided", paired=T, data = data_subset)

Get two vectors

Ford <- your_data_object$Head_IC[your_data_object$make == "Ford"]
Chev <- your_data_object$Head_IC[your_data_object$make == "Chevrolet"]

t.test(Ford, Chev, alternative = "two.sided", paired = TRUE)

There are many, many ways you could approach your problem, and you may want to look at ways to subset and transform your data that work with whatever set of tools you are using.

Benjamin
  • 16,897
  • 6
  • 45
  • 65