R:如何根据实验组和p值创建一个带均值和sd的表格?

Mik*_*ike 3 statistics r

我知道如何为个别变量做所有这些,但我需要报告大量变量的这些信息,并想知道是否有一种有效的方法来做到这一点.

Gre*_*now 5

tables封装使一切都在这个除了p值容易,p值是可行的.这是一个简单的例子:

> library(tables)
> iris2 <- iris[ iris$Species != 'versicolor', ]
> iris2$Species <- factor(iris2$Species)
> tmp <- tabular( Petal.Width+Petal.Length + Sepal.Width+Sepal.Length ~ Species* (mean+sd), data=iris2 )
> 
> tmp.p <- sapply( names(iris2)[1:4], function(x) t.test( iris2[[x]] ~ iris2$Species )$p.value )
> 
> tmp

              setosa        virginica       
              mean   sd     mean      sd    
 Petal.Width  0.246  0.1054 2.026     0.2747
 Petal.Length 1.462  0.1737 5.552     0.5519
 Sepal.Width  3.428  0.3791 2.974     0.3225
 Sepal.Length 5.006  0.3525 6.588     0.6359

> tmp2 <- cbind(tmp, tmp.p)
> colnames(tmp2) <- c('Setosa Mean','Setosa SD', 'Virginica Mean','Virginica SD',
+ 'P-value')
> tmp2
             Setosa Mean Setosa SD Virginica Mean Virginica SD P-value     
Sepal.Length 0.246       0.1053856 2.026          0.2746501    3.966867e-25
Sepal.Width  1.462       0.173664  5.552          0.5518947    4.570771e-09
Petal.Length 3.428       0.3790644 2.974          0.3224966    9.269628e-50
Petal.Width  5.006       0.3524897 6.588          0.6358796    2.437136e-48
Run Code Online (Sandbox Code Playgroud)

####编辑####

看起来像表格的更新版本做了更多的检查,这使得该cbind方法不再起作用(这可能是一件好事,因为如果排序不同,我不确定它是否正确匹配值).我没有找到一个简单的方法仍然使用cbind(虽然你可以转换为矩阵,填充标题的行,然后cbind).

这是另一种方法,它仍然是一个kludge,因为它硬编码函数中的物种变量(因此函数必须专门为其使用的每个表更新):

library(tables)
iris2 <- iris[ iris$Species != 'versicolor', ]
iris2$Species <- factor(iris2$Species)
P.value <- function(x) t.test(x ~ iris2$Species)$p.value
tmp <- tabular( Petal.Width+Petal.Length + Sepal.Width+Sepal.Length ~ Species* (mean+sd) + P.value, data=iris2 )
tmp
Run Code Online (Sandbox Code Playgroud)