How to get NA values instead of a “data are essentially constant” error in t.test in R

与世无争的帅哥 提交于 2019-12-24 08:17:31

问题


I have a large dataset of data from two groups. I want to compare it using a t.test and get a list of p.values for all the columns starting with F_, but because of the data in some columns, when I use my code:

TP_FN_ttest <- Map(t.test, x = TP[,grepl(paste0("^F_"),colnames(TP))], 
                           y = FN[,grepl(paste0("^F_"),colnames(FN))])
TP_FN_ttest.pval <- as.data.frame(sapply(TP_FN_ttest, '[[', 'p.value'))

I get an error:

Error in t.test.default(x = dots[[1L]][[508L]], y = dots[[2L]][[508L]]) : 
data are essentially constant

Is there a way to get a list of p.values and get NA's whenever the test cannot be applied instead of getting an error?


回答1:


You can use a try catch block like this:

tryCatch({
  ttest()
}, error = {
  somethingElse()
})


来源:https://stackoverflow.com/questions/45186571/how-to-get-na-values-instead-of-a-data-are-essentially-constant-error-in-t-tes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!