从多个t.test输出中获取某些结果以创建表

use*_*247 2 statistics r

我已经运行了48次t检验(手工编码而不是编写循环),并希望将这些t.tests的某些结果拼接出来,以创建一个我最感兴趣的事物表.

具体来说,我想保留这48个测试中每一个的p值,置信区间,x的平均值和y的平均值,然后构建一个结果表.

除了这里详细介绍的最佳答案之外,还有一种优雅,快速的方法吗?其中我会进行所有48次测试,并使用类似的东西获取所有三个所需的输出ttest$p.value?也许是循环?

下面是一个t检验的编码输入样本,然后是R提供的输出.

# t.test comparing means of Change_Unemp for 2005 government employment (ix)

lowgov6 <- met_res[met_res$Gov_Emp_2005 <= 93310, "Change_Unemp"]
highgov6 <- met_res[met_res$Gov_Emp_2005 > 93310, "Change_Unemp"]
t.test(lowgov6,highgov6,pool.sd=FALSE,na.rm=TRUE)  

Welch Two Sample t-test

data:  lowgov6 and highgov6
t = 1.5896, df = 78.978, p-value = 0.1159
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-0.1813909  1.6198399
sample estimates:
mean of x mean of y 
4.761224  4.042000 
Run Code Online (Sandbox Code Playgroud)

Tho*_*mas 6

将所有t检验保存到列表中:

tests <- list()
tests[[1]] <- t.test(lowgov6,highgov6,pool.sd=FALSE,na.rm=TRUE)
# repeat for all tests
# there are probably faster ways than doing all of that by hand

# extract your values using `sapply`
sapply(tests, function(x) {
     c(x$estimate[1],
       x$estimate[2],
       ci.lower = x$conf.int[1],
       ci.upper = x$conf.int[2],
       p.value = x$p.value)
})
Run Code Online (Sandbox Code Playgroud)

输出类似于以下内容:

                 [,1]        [,2]
mean of x  0.12095949  0.03029474
mean of y -0.05337072  0.07226999
ci.lower  -0.11448679 -0.31771191
ci.upper   0.46314721  0.23376141
p.value    0.23534905  0.76434012
Run Code Online (Sandbox Code Playgroud)

但是会有48列.t()如果您想要转置,您可以得到结果.