0

Hi all and thanks in advance for your help,

I have a data.frame in which I have multiple columns that I want to run a t.test over I'm trying to use the following code to run this

Cardenas1000.Rarefied.Sig<-lapply(Cardenas1000.Rarefied.Sig[,c(2:74)], function(x) t.test(x ~ Cardenas1000.Rarefied.Sig$Dis_Diag))

Unfortunately some of these columns are not appropriate for a t.test as they are more or less constant across the variable of interest, when R gets to these columns it simply stops the code running altogether Is there a way to either for the code to pass these columns, or to identify them so they can be removed prior to running the code?

Thanks again in advance!

David
  • 23
  • 9
  • If you just want to skip columns you can specify this in the list call, ie. `Cardenas1000.Rarefied.Sig[,c(2:34, 36:74)]` That would skip column 35 for example. – Croote Feb 27 '19 at 22:25
  • read up on tryCatch to catch error and supply an NA value for those columns. or use purrr and readup on ?possibly then you can do possibly(t.test, otherwise = NA) – infominer Feb 27 '19 at 22:37
  • and if that was too cryptic, read this https://stackoverflow.com/questions/2589275/how-to-tell-lapply-to-ignore-an-error-and-process-the-next-thing-in-the-list – infominer Feb 27 '19 at 22:47
  • Thanks, I've tried looking up tryCatch (which is an interesting function I wasn't aware of) but I'm not sure how I would wrap it in this code? – David Feb 28 '19 at 00:31
  • Actually I found out how to do it from this answer in the "fail with" part https://stackoverflow.com/questions/37474672/looping-through-t-tests-for-data-frame-subsets-in-r – David Feb 28 '19 at 02:09

0 Answers0