Reputation: 64064
I have the following data frame:
library(tidyverse)
dat <- tribble(
~sample, ~rep, ~area,
"S1", "01", 100,
"S1", "02", 200,
"S1", "01", 300,
"S2", "01", 10,
"S2", "02", 20,
"S2", "02", 30,
"S3", "01", 1.2,
"S3", "02", 2.3,
"S3", "02", 3.1
)
dat
#> # A tibble: 9 x 3
#> sample rep area
#> <chr> <chr> <dbl>
#> 1 S1 01 100.0
#> 2 S1 02 200.0
#> 3 S1 01 300.0
#> 4 S2 01 10.0
#> 5 S2 02 20.0
#> 6 S2 02 30.0
#> 7 S3 01 1.2
#> 8 S3 02 2.3
#> 9 S3 02 3.1
What I want to do is to create a function that takes a list of vectors with
a paired value and then calculate the P-value derived from t.test of area
column.
Fore example given this list:
wanted_pairs <- list(c("S1","S2"), c("S2","S3"))
We would like to generate this data frame:
S1 S2 0.0878746
S2 S3 0.07564237
Manually, the above P-values are calculated with:
t.test(dat[dat$sample=="S1",]$area, dat[dat$sample=="S2",]$area)$p.value
t.test(dat[dat$sample=="S2",]$area, dat[dat$sample=="S3",]$area)$p.value
How can I achieve that?
Upvotes: 1
Views: 56
Reputation: 887831
We can use a loop
library(dplyr)
lapply(wanted_pairs, function(x)
dat %>%
filter(sample %in% x) %>%
summarise(sample1 = unique(sample)[1], sample2 = unique(sample)[2],
pval = t.test(area[sample == sample1],
area[sample == sample2])$p.value)) %>%
bind_rows()
# A tibble: 2 x 3
# sample1 sample2 pval
# <chr> <chr> <dbl>
#1 S1 S2 0.0878746
#2 S2 S3 0.0897509
Upvotes: 1