如何在 data.frame 中运行多个 t.test? [英] How to run multiple t.test in a data.frame?

查看:31
本文介绍了如何在 data.frame 中运行多个 t.test?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据,

temp_data <- as.data.frame(matrix(rbinom(9*9, 1, 0.5), ncol=9, nrow =9))
colnames(temp_data) <- paste(rep(c("a","b","c"), each=3), rep(c(1,2,3), 3), sep = "")

我想对 data.frame 的因子运行多个 t.test 并获得一个显示此内容的输出文件

I want to run multiple t.test on the factors of the data.frame and get an output file that shows this

     factor.1 factor.2 p.value   
[1,] "a1"     "c1"     "value 1 "
[2,] "a2"     "c2"     "value 2 "
[3,] "a3"     "c3"     "value 3 "
[4,] "b1"     "c1"     "value 4 "
[5,] "b2"     "c2"     "value 5 "
[6,] "b3"     "c3"     "value 6 "

我怎样才能得到这个结果?

How can I get this results?

推荐答案

你可以这样做:

pa <- unlist(lapply(1:3, function(i) t.test(temp_data[, i], temp_data[, i + 6])$p.value))
pb <- unlist(lapply(1:3, function(i) t.test(temp_data[, i + 3], temp_data[, i + 6])$p.value))
p <- c(pa, pb)
p.table <- as.matrix(data.frame(factor.1 = colnames(temp_data[1:6]),
           factor.2 = rep(colnames(temp_data[7:9]), 2),
           p.value = p))
p.table
#      factor.1 factor.2 p.value      
# [1,] "a1"     "c1"     "0.652543514"
# [2,] "a2"     "c2"     "0.176463197"
# [3,] "a3"     "c3"     "0.176463197"
# [4,] "b1"     "c1"     "0.372458894"
# [5,] "b2"     "c2"     "0.003949773"
# [6,] "b3"     "c3"     "0.652543514"

这篇关于如何在 data.frame 中运行多个 t.test?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆