为数据框架的每一列计算一个样本t检验,并在表格中总结结果 [英] Compute one sample t-test for each column of a data frame and summarize results in a table

查看:210
本文介绍了为数据框架的每一列计算一个样本t检验,并在表格中总结结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的问题的一些示例数据:

Here is some sample data on my problem:

mydf <- data.frame(A = rnorm(20, 1, 5),
                   B = rnorm(20, 2, 5),
                   C = rnorm(20, 3, 5),
                   D = rnorm(20, 4, 5),
                   E = rnorm(20, 5, 5))

现在我想对数据框架的每一列执行单样本t检验,以证明其是否与零显着不同,如 t.test(mydf $ A),以及然后将每个列的平均值,t值和p值存储在新的数据框架中。所以结果应该是这样的:

Now I'd like to run a one-sample t-test on each column of the data.frame, to prove if it differs significantly from zero, like t.test(mydf$A), and then store the mean of each column, the t-value and the p-value in a new data.frame. So the result should look something like this:

      A    B    C    D    E
mean  x    x    x    x    x
t     x    x    x    x    x
p     x    x    x    x    x

我可以想到一些繁琐的方法来做到这一点,比如循环 mydf ,计算参数,然后循环遍历新的data.frame并插入值。

但是包含如 plyr 在这方面,不应该有更简洁优雅的方式来做到这一点吗?

I could definitely think of some tedious ways to do this, like looping through mydf, calculating the parameters, and then looping through the new data.frame and insert the values.
But with packages like plyr at hand, shouldn't there be a more concise and elegant way to do this?

任何想法都非常感激。

Any ideas are highly appreciated.

推荐答案

尝试这样的东西,然后从结果表中提取所需的结果:

Try something like this and then extract the results you want from the resulting table:

results <- lapply(mydf, t.test)
resultsmatrix <- do.call(cbind, results)
resultsmatrix[c("statistic","estimate","p.value"),]

给你: / p>

Gives you:

          A         B          C            D           E           
statistic 1.401338  2.762266   5.406704     3.409422    5.024222    
estimate  1.677863  2.936304   5.418812     4.231458    5.577681    
p.value   0.1772363 0.01240057 3.231568e-05 0.002941106 7.531614e-05

这篇关于为数据框架的每一列计算一个样本t检验,并在表格中总结结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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