我有如下所示的数据框:
set.seed(123)
df <- data.frame(factor1 = rep(c("A", "B"),50),
factor2 = rep(c("X","X", "Y", "Y"),25),
value = rnorm(100))
我想计算 factor1:factor2 对的一些汇总值。我使用以下方法计算了平均值和标准差:
summary <- as.matrix(cast(df, factor1~factor2, mean))
summary.sd <- as.matrix(cast(df, factor1~factor2, sd))
summary.table <- t(rbind(summary, summary.sd))
colnames(summary.table) <- c("A.mean", "B.mean", "A.sd", "B.sd")
但是我想将比较 A 与 B 的 t.test 中的 p 值添加到 summary.table 中。到目前为止,我已经这样做了,但这不仅没有写入 summary.table,而且我无法获得名称与它一起打印的 factor2 变量:
for (measurement in levels(df$factor2)) print(t.test(value~factor1, data=subset(df, factor2==measurement)))
我认为必须有一些简单的方法来做到这一点,或者也许我不知道的一个包会使这更简单。