遍历两个表(矩阵)匹配列并在 R 中应用函数

walking through two tables (matrices) matching columns and applying a function in R

所以我有两个矩阵。让我们将它们命名为对照和患者。每行是一个样本,每列是某种蛋白质的浓度。它看起来像这样:

        V1    V2    V3    V4     V5     V6    V7    V8    V9     V10    V11
sample1  1533.34  9.88  6.82 17.88  70.75 350.07 20.67 13.96 10.17  711.02 114.06
sample2  2311.30 12.74  6.82 17.88  80.71 505.96 34.36 19.66 18.70  863.70 181.43
sample3  1314.83 11.39 18.12 41.26 104.36 278.17 40.25 27.12 41.34 1100.00 160.83

这只是一小部分,我其实还有更多的价值。我想按列将其与另一个匹配的 table 进行比较。附带问题,假设数据呈正态分布,在这种情况下使用 t 检验是否正确?反正。我试过 apply() 函数:

apply(controls,2,function(x) t.test(x, patients)$p.value)

我得到了一些结果。但是我怀疑我是否正确使用了该功能。它是否像预期的那样匹配两个 table 中的两列?还是我用错了?

编辑哦,是的。这绝对是不正确的。作为第二列的平均值 table 始终保持不变。

尝试

 mapply(function(x,y) t.test(x,y)$p.value, 
         as.data.frame(controls), as.data.frame(patients))
 #       V1        V2        V3        V4        V5        V6        V7        V8 
 #0.8481788 1.0000000 0.4605294 1.0000000 0.6436604 1.0000000    1.0000000 1.0000000 
 #       V9       V10       V11 
 #1.0000000 1.0000000 1.0000000 

假设“对照”和“患者”是matrix

数据

controls <- structure(c(1253, 2311.3, 1314.83, 9.88, 12.74, 11.39, 
20.8, 
6.82, 18.12, 17.88, 17.88, 41.26, 70.75, 53.5, 104.36, 350.07, 
505.96, 278.17, 20.67, 34.36, 40.25, 13.96, 19.66, 27.12, 10.17, 
18.7, 41.34, 711.02, 863.7, 1100, 114.06, 181.43, 160.83),
.Dim = c(3L, 
11L), .Dimnames = list(c("sample1", "sample2", "sample3"), c("V1", 
"V2", "V3", "V4", "V5", "V6", "V7", "V8", "V9", "V10", "V11")))

patients <- structure(c(1533.34, 2311.3, 1314.83, 9.88, 12.74, 11.39, 
6.82, 
6.82, 18.12, 17.88, 17.88, 41.26, 70.75, 80.71, 104.36, 350.07, 
505.96, 278.17, 20.67, 34.36, 40.25, 13.96, 19.66, 27.12, 10.17, 
18.7, 41.34, 711.02, 863.7, 1100, 114.06, 181.43, 160.83),
.Dim = c(3L, 
11L), .Dimnames = list(c("sample1", "sample2", "sample3"), c("V1", 
"V2", "V3", "V4", "V5", "V6", "V7", "V8", "V9", "V10", "V11")))

我在解析你的问题时遇到了一些问题,但我可能有答案。

如果您正在寻找两个等维矩阵之间的列匹配率,有一种非常简单的方法可以做到这一点:

colMeans(controls==patients)

求矩阵之间的绝对匹配数:

colSums(controls==patients)

假设"patients"和"controls"是data.frame,试试:

results <- dplyr::summarise_each(patients, funs(t.test(., controls$.)$p.value))