Ordering a matrix by t.test pvalue

Hi everybody, I am a beginner in R and am not comfortable with functions.
Here is my problem : I have a matrix with 26 samples and 56.000 lines of measures ( on 4/4 in my example)

counts <- data.frame(stringsAsFactors=FALSE,
Sample1 = c(4, 8, 12, 16),
Sample2 = c(12,15,16,12),
Sample3 = c(14,16,7,8),
Sample4 = c(12,13,12,14)
)
13 samples belong to "group E" and 13 other belong to "group M" (2v2 in the example)
I would like to create a function returning me a vector with the p.value of the ttest between group E and group M.
I tried with an apply, but don't find a way for the fonction inside...
Could you help me please to get ou the problem ?
Regards,

Simon

Hi @SimonG. Use the pmap_dbl to iterate rows to t.test and return the p.value.

library(tidyverse)

counts <- data.frame(stringsAsFactors=FALSE,
                     Sample1 = c(4, 8, 12, 16),
                     Sample2 = c(12,15,16,12),
                     Sample3 = c(14,16,7,8),
                     Sample4 = c(12,13,12,14)
)

pmap_dbl(counts, function(...){t.test(c(...) ~ factor(c("E", "E", "M", "M")))$p.value})
#> [1] 0.4214363 0.5450249 0.3005739 0.5038379

Created on 2019-10-15 by the reprex package (v0.3.0)

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.