首页 > 解决方案 > t 检验的“数据基本不变”错误

问题描述

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

这是我的 R 代码,用于对一组关于细菌抗生素耐药性的数据进行 t 检验。这给了我错误代码:

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.

不知道我做错了什么

标签: r

解决方案


我刚刚遇到了同样的错误。这可能是由于每组中的所有值都相同。

所以只需再写两个“if else”。对我来说,我做到了

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

推荐阅读