Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The <code>tables</code> package makes everything in this except the p-values easy, and the p-values are doable. Here is a quick example:</p> <pre><code>&gt; library(tables) &gt; iris2 &lt;- iris[ iris$Species != 'versicolor', ] &gt; iris2$Species &lt;- factor(iris2$Species) &gt; tmp &lt;- tabular( Petal.Width+Petal.Length + Sepal.Width+Sepal.Length ~ Species* (mean+sd), data=iris2 ) &gt; &gt; tmp.p &lt;- sapply( names(iris2)[1:4], function(x) t.test( iris2[[x]] ~ iris2$Species )$p.value ) &gt; &gt; 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 &gt; tmp2 &lt;- cbind(tmp, tmp.p) &gt; colnames(tmp2) &lt;- c('Setosa Mean','Setosa SD', 'Virginica Mean','Virginica SD', + 'P-value') &gt; 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 </code></pre> <p><strong>#### Edit ####</strong></p> <p>It looks like newer versions of tabular do more checks which makes the <code>cbind</code> 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 <code>cbind</code> (though you could convert to a matrix, pad the rows for the headers, then <code>cbind</code>).</p> <p>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):</p> <pre><code>library(tables) iris2 &lt;- iris[ iris$Species != 'versicolor', ] iris2$Species &lt;- factor(iris2$Species) P.value &lt;- function(x) t.test(x ~ iris2$Species)$p.value tmp &lt;- tabular( Petal.Width+Petal.Length + Sepal.Width+Sepal.Length ~ Species* (mean+sd) + P.value, data=iris2 ) tmp </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload