Hi there,
Thanks, the data and code really helped!
Here is what I think you are trying to do:
library(tidyverse)
#The data
Forumhelp <- tribble(
~Genotyp, ~Medication, ~ANP.Expr., ~BNP.Expr.,
"db+", "contr", "0,01298", "1,02997",
"db+", "contr", "3,28628", "2,97766",
"db+", "contr", "0,92293", "2,51611",
"db+", "contr", "0,88408", "4,54664",
"db+", "treat", "1,71247", "2,02679",
"db+", "treat", "1,24132", "1,93011",
"db+", "treat", "2,19683", "1,36958",
"db+", "treat", "3,18670", "3,83384",
"dbdb", "contr", "0,18560", "0,23984",
"dbdb", "contr", "0,07026", "0,43382",
"dbdb", "contr", "0,16512", "0,08359",
"dbdb", "contr", "0,31011", "0,19674",
"dbdb", "treat", "0,12522", "0,11293",
"dbdb", "treat", "1,43110", "0,24108",
"dbdb", "treat", "0,03317", "0,28575",
"dbdb", "treat", "0,32317", "0,16888",
)
#Clean up the data
# make it point instead of comma decimal
# convert from string to number
Forumhelp = Forumhelp %>% mutate(
ANP.Expr. = as.numeric(str_replace(ANP.Expr., ",", ".")),
BNP.Expr. = as.numeric(str_replace(BNP.Expr., ",", ".")),
)
#Group by Genotyp and do the t-test
Forumhelp = Forumhelp %>% group_by(Genotyp) %>%
summarise(
as.data.frame(t(unlist(
t.test(ANP.Expr. ~ Medication)
)))
)
Forumhelp
#> # A tibble: 2 x 13
#> Genotyp statistic.t parameter.df p.value conf.int1 conf.int2 `estimate.mean ~
#> * <chr> <chr> <chr> <chr> <chr> <chr> <chr>
#> 1 db+ -0.9898274~ 4.875534511~ 0.3688~ -2.92173~ 1.306207~ 1.2765675
#> 2 dbdb -0.9030789~ 3.139528466~ 0.4303~ -1.31066~ 0.719880~ 0.1827725
#> # ... with 6 more variables: `estimate.mean in group treat` <chr>,
#> # `null.value.difference in means` <chr>, stderr <chr>, alternative <chr>,
#> # method <chr>, data.name <chr>
Created on 2021-02-12 by the reprex package (v1.0.0)
So the core function here is the group_by()
from the dplyr package (part of the tidyverse). It will split the data according to the groups you define, and process the rest in those splits. I then used the summarise()
function from the same package to use each chunk of data for the t-test. This t-test function generates a list, so I used some tricks to convert the list into a data.frame which will then be pasted together by the summary function with one row per group.
You could also do this with a loop instead or use a mapping function, where you merge the data frames in the end or just keep a list, you would use this function instead then:
Forumhelp = Forumhelp %>% group_by(Genotyp) %>%
group_map(
~t.test(ANP.Expr. ~ Medication, data = .x)
)
Please have a look at the power of dplyr if you like to learn more.
Hope this helps,
PJ