我有以下数据框:
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
我想要做的是创建一个带有向量列表的函数
配对值,然后计算从area
列的t.test派生的P值。
给出这个清单的前例:
wanted_pairs <- list(c("S1","S2"), c("S2","S3"))
我们想生成这个数据框:
S1 S2 0.0878746
S2 S3 0.07564237
手动,上述P值计算如下:
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
我怎样才能做到这一点?
答案 0 :(得分:1)
我们可以使用循环
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