Genevieve Rice
Genevieve Rice

Reputation: 61

"data are essentially constant" error with t test

t.test(antibioticdata$Bacteria,
       antibioticdata$Inhibition, 
       alternative = c("two.sided"),
       paired = FALSE, 
       var.equal = FALSE)

Here is my R code to make a t-test for a set of data on antibiotic resistance of bacteria. This gives me the error code:

Error in if (stderr < 10 * .Machine$double.eps * max(abs(mx), abs(my))) stop("data are essentially constant") : 
  missing value where TRUE/FALSE needed
In addition: Warning messages:
1: In mean.default(x) : argument is not numeric or logical: returning NA
2: In var(x) :
  Calling var(x) on a factor x is deprecated and will become an error.
  Use something like 'all(duplicated(x)[-1L])' to test for a constant vector.

not sure what I am doing wrong

Upvotes: 3

Views: 13962

Answers (1)

flyboyleo
flyboyleo

Reputation: 51

I just met the same error. It's probably due to all the values in each group are the same.

So just write two more "if else". For me, I did

library("greenbrown") 

apply(data.table, 1, function(x){

  if(AllEqual(x[1:9])){return(1)}

  else if(AllEqual(x[1:4]) & AllEqual(x[5:9])){return(0)} else {

  t.test(as.numeric(x[1:4]), as.numeric(x[5:9]))->t.results

  return(t.results$p.value)
  }
})->P.for.data.table

Upvotes: 3

Related Questions