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 w
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
#### Edit ####
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