0

This is my situation: 11 variables columns vs 1 column.

I want to test the single column[which it's sorted into 0 and 1s to create two groups] for each of the 11 columns. Single column and 11 columns have different lenghts and are not homoschedastic.

I tried with:

> TTest1 <- t.test(A$Column1, Bsorted0, var.equal = FALSE)
> View(TTest1) 

which gave me a p-value of 7.681668e-05

but how can I loop this in order to have A$column1,2,3,4,5...each one tested with Bsorted0? Is it the right approach?

Thanks

Filippo
  • 33
  • 1
  • 7
  • You might want to run a `var.test()` to see whether the var.equal flag is TRUE or FALSE – Diego Oct 15 '21 at 11:22

1 Answers1

1

Loop through the list with lapply calling the anonymous function \(x) (R4.1.0) or function(x) (previous versions of R).

TTest1_list <- lapply(A, \(x) {
  tryCatch(t.test(x, Bsorted0, var.equal = FALSE),
           error = \(e) e)
})

names(TTest1_list) <- names(A)
ok <- !sapply(TTest1_list, inherits, "error")
Err_list <- TTest1_list[!ok]
TTest1_list <- TTest1_list[ok]

Then, to extract the statistic or p-values, use sapply on the tests' list.

stat <- sapply(TTest1_list, `[[`, 'statistic')
pval <- sapply(TTest1_list, `[[`, 'p.value')

And to see the errors, apply conditionMessage to the errors list.

lapply(Err_list, conditionMessage)
Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
  • Hi! Thanks for your answer.. It gives me this error: Error in if (stderr < 10 * .Machine$double.eps * max(abs(mx), abs(my))) stop("data are essentially constant") missing value where it is required TRUE/FALSE – Filippo Oct 15 '21 at 12:59