2

I have a data frame called trainingData.In this data frame, I have two variables called Type and Method which are both categories.

when i try to run the following code

res<-t.test(trainingData$Type~trainingData$Method,data=trainingData,paired=TRUE)

I am getting an error saying "Grouping factor must have exactly two levels"

I have found similar questions in stackoverflow but nothing gave me a proper solution.

Please Help!!!

  • 1
    Hard to say without a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). But the error says your grouping variable doesn't have two levels--is that the case? Also, if you're supplying a value to the `data` argument, you should just use `Type ~ Method`, since it's already clear what dataframe those columns are in – camille Nov 15 '18 at 16:32
  • Have a look at ?t.test(). You misspecified the arguments. try t.test(Type~Method, data=TrainingData) – yoland Nov 15 '18 at 16:35
  • Hey @camille yes that is the case. And also in data argument i tried giving Type~Method, but even that is giving me an error:Error in as.data.frame.default(data) : cannot coerce class ‘"formula"’ to a data.frame – Bharath Naidu Yenumula Nov 15 '18 at 16:38
  • @yoland I tried your suggestion. But again giving me the same error. – Bharath Naidu Yenumula Nov 15 '18 at 16:40
  • There isn't a whole lot to do without a reproducible question, like I said. But look at the docs for `t.test` to see how you should be calling this function. The formula isn't the data argument, it's the formula argument – camille Nov 15 '18 at 16:42

1 Answers1

5

The problem is that your grouping variable has more than two levels, when the t.test requires that you cannot have more than two levels.

Here is a reproduction of your error:

library(tidyverse)


##This will reproduce your error

##Create some fake data
data_test <- tibble(measure = c(rnorm(100,30,5),rnorm(100,15,5)),
       group = factor(rep(c("A","B","C"),c(95,95,10))))


table(data_test$group) ##Notice that you have three levels

#Try to run the test
t.test(measure~group, data = data_test, paired = TRUE)

Here is an example that will run

##This will not result in a error, because you only have two groups

data_test2 <- tibble(measure = c(rnorm(100,30,5),rnorm(100,15,5)),
                    group = factor(rep(c("A","B"),c(100,100))))

table(data_test$group) ##Notice that you have the required two levels
t.test(measure~group, data = data_test2,paired = TRUE) ##Test will now run

Takeaway: Check the number of levels in your data. If there are more than two, recode or delete them.

Henry Cyranka
  • 2,970
  • 1
  • 16
  • 21