Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It looks like you want to apply a number of different calculations to some data, grouping it by one field (in the example, by state)? </p> <p>There are many ways to do this. See <a href="https://stackoverflow.com/questions/1407449/for-each-group-summarise-means-for-all-variables-in-dataframe-ddply-split/">this related question</a>. </p> <p>You could use Hadley Wickham's <code>reshape</code> package (see <a href="http://had.co.nz/reshape/" rel="nofollow noreferrer">reshape homepage</a>). For instance, if you wanted the mean, sum, and count functions applied to some data grouped by a value (this is meaningless, but it uses the airquality data from reshape):</p> <pre><code>&gt; library(reshape) &gt; names(airquality) &lt;- tolower(names(airquality)) &gt; # melt the data to just include month and temp &gt; aqm &lt;- melt(airquality, id="month", measure="temp", na.rm=TRUE) &gt; # cast by month with the various relevant functions &gt; cast(aqm, month ~ ., function(x) c(mean(x),sum(x),length(x))) month X1 X2 X3 1 5 66 2032 31 2 6 79 2373 30 3 7 84 2601 31 4 8 84 2603 31 5 9 77 2307 30 </code></pre> <p>Or you can use the <code>by()</code> function. Where the index will represent the states. In your case, rather than apply one function (e.g. mean), you can apply your own function that will do multiple tasks (depending upon your needs): for instance, <code>function(x) { c(mean(x), length(x)) }</code>. Then run <code>do.call("rbind"</code> (for instance) on the output.</p> <p>Also, you might give some consideration to using a reporting package such as Sweave (with xtable) or <a href="http://cran.r-project.org/web/packages/brew/index.html" rel="nofollow noreferrer">Jeffrey Horner's brew package</a>. There is a <a href="http://learnr.wordpress.com/2009/09/09/brew-creating-repetitive-reports/" rel="nofollow noreferrer">great post on the learnr blog about creating repetitive reports</a> that shows how to use it. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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