R:如何根据实验组以及 p 值创建带有均值和 sd 的表? [英] R: how can I create a table with mean and sd according to experimental group alongside p-values?

查看:19
本文介绍了R:如何根据实验组以及 p 值创建带有均值和 sd 的表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何为单个变量执行所有这些操作,但我需要报告大量变量的此信息,并且想知道是否有一种有效的方法可以做到这一点.

I know how I can do all that for individual variables but I need to report this information for a large number of variables and would like to know if there is an efficient way to do this.

推荐答案

tables 包使除 p 值之外的所有内容都变得简单,并且 p 值是可行的.这是一个简单的例子:

The tables package makes everything in this except the p-values easy, and the p-values are doable. Here is a quick example:

> 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

#### 编辑 ####

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

It looks like newer versions of tabular do more checks which makes the cbind approach not work any more (and this could be a good thing, since I am not sure that it was properly matching the values if the ordering was different). I did not find a simple way to still do this using cbind (though you could convert to a matrix, pad the rows for the headers, then cbind).

这是另一种有效的方法,它仍然有点麻烦,因为它对函数中的物种变量进行了硬编码(因此必须针对使用它的每个表专门更新该函数):

Here is another approach that works, it is still a bit of a kludge since it hardcodes the species variable in the function (and the function would therefore have to be updated specifically for each table it is used in):

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

这篇关于R:如何根据实验组以及 p 值创建带有均值和 sd 的表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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