Reputation: 3545
I have data that looks like this:
Replicate Group Value
1 A 1.0
1 A 1.1
1 A 1.1
1 B 2.0
1 B 2.0
1 B 2.2
2 A 1.1
2 A 1.2
2 A 0.9
2 B 2.2
2 B 2.4
I would like to use t.test()
to get the difference in means between A and B, p-value, and 95% CI for each separate Replicate. How can I most easily do this?
Here's code to get the toy example above into a data frame:
df = data.frame("Replicate"=c(1,1,1,1,1,1,2,2,2,2,2), "Group"=c("A","A","A","B","B","B","A","A","A","B","B"), "Value"= c(1.0, 1.1, 1.1, 2.0, 2.0, 2.2, 1.1, 1.2, 0.9, 2.2, 2.4))
Thanks for any help!
Upvotes: 0
Views: 159
Reputation: 50718
I'm not entirely sure I understand what you mean by "for each separate Replicate". To characterise the difference in means between Value
s for the different Group
s we can specify the formula
in t.test
as
ttest <- t.test(Value ~ Group, data = df)
ttest
#
#Welch Two Sample t-test
#
#data: Value by Group
#t = -12.729, df = 6.4248, p-value = 8.52e-06
#alternative hypothesis: true difference in means is not equal to 0
#95 percent confidence interval:
#-1.3001853 -0.8864814
#sample estimates:
#mean in group A mean in group B
# 1.066667 2.160000
The 95% CI is given by
ttest$conf.int
#[1] -1.3001853 -0.8864814
#attr(,"conf.level")
#[1] 0.95
On second thought, if you really want to perform separate t-tests for measurements from Replicate
1 and 2, we can group_by
Replicate
, nest
the data per Replicate
, and then map
a t.test
to the nested data. We can then extract relevant quantities from the t.test
per Replicate
:
library(tidyverse)
df %>%
group_by(Replicate) %>%
nest() %>%
mutate(ttest = map(data, ~t.test(Value ~ Group, .x))) %>%
transmute(
Replicate,
diff = map_dbl(ttest, ~.x$estimate[1] - .x$estimate[2]),
pval = map_dbl(ttest, ~.x$p.value),
CI = map(ttest, ~data.frame(CI.low = .x$conf.int[1], CI.high = .x$conf.int[2]))) %>%
unnest()
## A tibble: 2 x 5
# Replicate diff pval CI.low CI.high
# <dbl> <dbl> <dbl> <dbl> <dbl>
#1 1 -1. 0.000990 -1.24 -0.760
#2 2 -1.23 0.00599 -1.72 -0.746
Upvotes: 1