{"id":232,"date":"2013-06-09T00:52:28","date_gmt":"2013-06-09T05:52:28","guid":{"rendered":"http:\/\/homepages.uc.edu\/~yaozo\/wordpress\/?p=232"},"modified":"2013-06-09T00:52:28","modified_gmt":"2013-06-09T05:52:28","slug":"summary-statistics-by-two-or-more-factor-variables","status":"publish","type":"post","link":"https:\/\/zhuoyao.net\/index.php\/2013\/06\/09\/summary-statistics-by-two-or-more-factor-variables\/","title":{"rendered":"Summary statistics by two or more factor variables"},"content":{"rendered":"<p>How about this, still using\u00a0<code>tapply()<\/code>? It&#8217;s more versatile than you knew!<\/p>\n<pre><code>with(mtcars, tapply(mpg, list(cyl, gear), sum))\n#       three  four five\n# four   21.5 215.4 56.4\n# six    39.5  79.0 19.7\n# eight 180.6    NA 30.8<\/code><\/pre>\n<p>Or, if you&#8217;d like the printed output to be a bit more interpretable:<\/p>\n<pre><code>with(mtcars, tapply(mpg, list(\"Cylinder#\"=cyl, \"Gear#\"=gear), sum))<\/code><\/pre>\n<hr \/>\n<p>If you want to use more than two cross-classifying variables, the idea&#8217;s exactly the same. The results will then be returned in a 3-or-more-dimensional array:<\/p>\n<pre><code>A &lt;- with(mtcars, tapply(mpg, list(cyl, gear, carb), sum))\n\ndim(A)\n# [1] 3 3 6\nlapply(1:6, function(i) A[,,i]) # To convert results to a list of matrices\n\n# But eventually, the curse of dimensionality will begin to kick in...\ntable(is.na(A))\n# FALSE  TRUE \n#    12    42 <\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>How about this, still using\u00a0tapply()? It&#8217;s more versatile than you knew! with(mtcars, tapply(mpg, list(cyl, gear), sum)) # three four five # four 21.5 215.4 56.4&hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[],"class_list":["post-232","post","type-post","status-publish","format-standard","hentry","category-r"],"_links":{"self":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts\/232","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/comments?post=232"}],"version-history":[{"count":0,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts\/232\/revisions"}],"wp:attachment":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/media?parent=232"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/categories?post=232"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/tags?post=232"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}