{"id":315,"date":"2013-11-14T12:11:26","date_gmt":"2013-11-14T17:11:26","guid":{"rendered":"http:\/\/homepages.uc.edu\/~yaozo\/wordpress\/?p=315"},"modified":"2013-11-14T12:11:26","modified_gmt":"2013-11-14T17:11:26","slug":"grouping-and-sorting-in-r","status":"publish","type":"post","link":"https:\/\/zhuoyao.net\/index.php\/2013\/11\/14\/grouping-and-sorting-in-r\/","title":{"rendered":"Grouping and Sorting in R"},"content":{"rendered":"<p>Basically, using R I would like to take a data set that looks like this:<\/p>\n<p><img decoding=\"async\" alt=\"Simple Data Set\" src=\"http:\/\/i.stack.imgur.com\/gY7Ct.png\" \/><\/p>\n<p>And transform it to look like this:<\/p>\n<p><img decoding=\"async\" alt=\"Pivoted Data Set\" src=\"http:\/\/i.stack.imgur.com\/iksGG.png\" \/><\/p>\n<p>Notice that the final table groups points by team and then sorts the summary data by the number of points scored. Learning how to do this in R would be a huge time saver.<\/p>\n<p>The two functions you will need &#8230; once you have an example &#8230; are\u00a0<code>xtabs<\/code>\u00a0and\u00a0<code>ftable<\/code>. First we convert an existing table to a dataframe and then note that the counts are now in a variable named &#8220;Freq&#8221;. Then we re-tabulate to a smaller number of dimensions:<\/p>\n<pre><code>&gt; Tdf &lt;- as.data.frame(Titanic, stringsAsFactors=FALSE)\n&gt; str(Tdf)\n'data.frame':   32 obs. of  6 variables:\n $ Class   : chr  \"1st\" \"2nd\" \"3rd\" \"Crew\" ...\n $ Sex     : chr  \"Male\" \"Male\" \"Male\" \"Male\" ...\n $ Age     : chr  \"Child\" \"Child\" \"Child\" \"Child\" ...\n $ Survived: chr  \"No\" \"No\" \"No\" \"No\" ...\n $ Freq    : num  0 0 35 0 0 0 17 0 118 154 ...\n\n&gt; ftable( xtabs(Freq ~ Class+Sex, Tdf) , row.vars=c(\"Class\", \"Sex\") )\nClass Sex        \n1st   Female  145\n      Male    180\n2nd   Female  106\n      Male    179\n3rd   Female  196\n      Male    510\nCrew  Female   23\n      Male    862<\/code><\/pre>\n<p>I haven&#8217;t gotten very many points for it but I did at one time demonstrate on SO how to do sub-totals and grand totals:<\/p>\n<p><a href=\"http:\/\/stackoverflow.com\/questions\/5982546\/r-calculating-column-sums-row-sums-as-an-aggregation-from-a-dataframe\/5982943#5982943\">R: calculating column sums &amp; row sums as an aggregation from a dataframe<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Basically, using R I would like to take a data set that looks like this: And transform it to look like this: Notice that the&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-315","post","type-post","status-publish","format-standard","hentry","category-r"],"_links":{"self":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts\/315","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=315"}],"version-history":[{"count":0,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts\/315\/revisions"}],"wp:attachment":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/media?parent=315"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/categories?post=315"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/tags?post=315"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}