使用startsWith + mapply 的基本 R 选项
WT <- data.frame(t(df[startsWith(names(df), "WT")]))
KO <- data.frame(t(df[startsWith(names(df), "KO")]))
df$p.value <- mapply(function(x, y) t.test(x, y)$p.value, WT, KO)
给予
> df
Gene WT1 WT2 WT3 KO1 KO2 KO3 p.value
1 BIG2 -4.46 -5.250 -5.0100 -4.590 -3.470 -5.16 0.432649677
2 CAVN1 -4.71 -4.780 -4.6900 -4.530 -4.620 -5.14 0.865600809
3 HVM03 -5.31 -5.630 -3.9800 -0.418 -0.194 -4.21 0.108804979
4 DYN1 -2.09 -0.292 -0.0488 -5.130 -5.900 -4.96 0.009712383
5 ACSA 4.62 4.420 4.6200 -5.320 -3.830 -4.08 0.002216407
数据
> dput(df)
structure(list(Gene = c("BIG2", "CAVN1", "HVM03", "DYN1", "ACSA"
), WT1 = c(-4.46, -4.71, -5.31, -2.09, 4.62), WT2 = c(-5.25,
-4.78, -5.63, -0.292, 4.42), WT3 = c(-5.01, -4.69, -3.98, -0.0488,
4.62), KO1 = c(-4.59, -4.53, -0.418, -5.13, -5.32), KO2 = c(-3.47,
-4.62, -0.194, -5.9, -3.83), KO3 = c(-5.16, -5.14, -4.21, -4.96,
-4.08)), class = "data.frame", row.names = c("1", "2", "3", "4",
"5"))