I have a tibble with 2 columns: first one is a factor (6 possible values) and second one is a gene expression level (double). I would like to perform a t-test for each group. To do this, I need the expression level from the current group (x parameter in t.test) and the expression level from everything but the current group (y parameter in t.test).
Below is an example that works only for the first factor. I would like to change the filter line so that I get the proper y.
I had 2 ideas to do that:
- Retrieve the name of the group. From what I could find on Stack Overflow this seems to be not possible.
- Do a set difference between the whole dataset and the .x but I didn't succeed.
library(tidyverse) library(broom) # A sample of my data data <- tribble ( ~value, ~expr_level, "BCR", 0.564, "BCR", 0.841, "E2A", 0.214, "E2A", 0.147, "MLL", 0.451, "MLL", 0.411 ) data %>% group_by(value) %>% group_map(~ t.test(x = .x %>% select(expr_level), y = data %>% filter(value != "BCR") %>% select(expr_level)) %>% tidy() %>% select(statistic)) #> [] #> # A tibble: 1 x 1 #> statistic #> <dbl> #> 1 2.53 #> #> [] #> # A tibble: 1 x 1 #> statistic #> <dbl> #> 1 -1.54 #> #> [] #> # A tibble: 1 x 1 #> statistic #> <dbl> #> 1 1.63
Created on 2021-05-01 by the reprex package (v2.0.0)
Thanks for your input.