<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/rcase4base-data-aggregation-with-base-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://jozefhajnala.gitlab.io/r/r003-aggregation/"> Jozef's Rblog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<div>
<h1>Introduction</h1>
<p>In the previous articles of the R:case4base series we discussed and learned</p>
<ul>
<li>how to <a rel="nofollow" href="https://jozefhajnala.gitlab.io/r/r003-aggregation/..%5Cr001-reshape" target="_blank">reshape data with base R</a> to a form that is practical for our use and</li>
<li>how to <a rel="nofollow" href="https://jozefhajnala.gitlab.io/r/r003-aggregation/..%5Cr002-data-manipulation" target="_blank">subset data</a> to get the relevant parts of it with base R.</li>
</ul>
<p>In this one, we will look at aggregation techniques using base R’s <code>stats::aggregate</code> generic function, focusing on the method for data frames. This will allow us to easily and safely create simple aggregations, but also provide a framework for completely custom aggregation functionality defined as separate functions that can be properly documented and unit tested.</p>
</div>
<div>
<h1>Contents</h1>
<ol>
<li><a rel="nofollow" href="https://jozefhajnala.gitlab.io/r/r003-aggregation/#how-to-use-this-article" target="_blank">How to use this article</a></li>
<li><a rel="nofollow" href="https://jozefhajnala.gitlab.io/r/r003-aggregation/#simple-aggregations" target="_blank">Simple aggregations</a></li>
<li><a rel="nofollow" href="https://jozefhajnala.gitlab.io/r/r003-aggregation/#grouping-by-more-variables-and-small-tweaks" target="_blank">Grouping by more variables and small tweaks</a></li>
<li><a rel="nofollow" href="https://jozefhajnala.gitlab.io/r/r003-aggregation/#using-aggregate-as-a-framework-with-custom-aggregation-functions" target="_blank">Using aggregate as a framework with custom aggregation functions</a></li>
<li><a rel="nofollow" href="https://jozefhajnala.gitlab.io/r/r003-aggregation/#advanced-details-of-aggregate-use" target="_blank">Advanced details of aggregate use</a></li>
<li><a rel="nofollow" href="https://jozefhajnala.gitlab.io/r/r003-aggregation/#aggregates-methods-for-other-object-classes" target="_blank">Aggregate’s methods for other object classes</a></li>
<li><a rel="nofollow" href="https://jozefhajnala.gitlab.io/r/r003-aggregation/#alternatives-to-base-r" target="_blank">Alternatives to base R</a></li>
<li><a rel="nofollow" href="https://jozefhajnala.gitlab.io/r/r003-aggregation/#tldr---just-want-the-code" target="_blank">TL;DR – Just want the code</a></li>
<li><a rel="nofollow" href="https://jozefhajnala.gitlab.io/r/r003-aggregation/#exercises" target="_blank">Exercises</a></li>
<li><a rel="nofollow" href="https://jozefhajnala.gitlab.io/r/r003-aggregation/#references" target="_blank">References</a></li>
<li><a rel="nofollow" href="https://jozefhajnala.gitlab.io/r/r003-aggregation/#exercise-answers" target="_blank">Exercise answers</a></li>
</ol>
</div>
<div>
<h1>How to use this article</h1>
<ol>
<li>This article is best used with an R session opened in a window next to it – you can test and play with the code yourself instantly while reading. Assuming the author did not fail miserably, the code will work as-is even with vanilla R, no packages or setup needed – it is a <code>case4base</code> after all!</li>
<li>If you have no time for reading, you can <a rel="nofollow" href="https://jozefhajnala.gitlab.io/r/post/data/r003-aggregation.r" target="_blank">click here to get just the code with commentary</a></li>
</ol>
<p>First, let’s read in yearly data on gross disposable income of household in the EU countries into R (<a rel="nofollow" href="https://jozefhajnala.gitlab.io/r/post/data/ESA2010_GDI.csv" target="_blank">click here to download</a>) and reshape them to get a nice, long format data to work with:</p>
<pre><code>gdi &lt;- read.csv(
  stringsAsFactors = FALSE
, file = &quot;https://jozefhajnala.gitlab.io/r/post/data/ESA2010_pretty.csv&quot;
)

gdi &lt;- reshape(data = gdi
             , direction = &quot;long&quot; # we are going from wide to long
             , varying = 2:67     # columns that will be stacked into 1
             , idvar = &quot;country&quot;  # identifying the subject in rows
             )</code></pre>
</div>
<div>
<h1>Simple aggregations</h1>
<p>In this paragraph, we will try to show how to perform simple aggregation on data.frames. As the first example, let us look at the mean gross saving across the years per country:</p>
<pre><code>aggregate.data.frame(x = gdi[&quot;GrossSaving&quot;]
                   , by = list(country = gdi[[&quot;country&quot;]])
                   , FUN = mean
                   )</code></pre>
<pre><code>##           country GrossSaving
## 1         Austria  24724.6227
## 2         Belgium  28961.7136
## 3        Bulgaria  -1711.6136
## 4         Croatia          NA
## 5          Cyprus          NA
## 6  Czech Republic 208404.0000
## 7         Denmark  53667.7273
## 8         Estonia    487.1409
## 9           EU 28          NA
## 10   Euro area 19          NA
## 11        Finland   7656.7727
## 12         France 169311.6818
## 13        Germany 265215.6818
## 14         Greece   5289.8464
## 15        Hungary          NA
## 16        Iceland          NA
## 17        Ireland   5831.3136
## 18          Italy 135086.8591
## 19         Latvia    147.1718
## 20      Lithuania    394.4595
## 21     Luxembourg   2510.5136
## 22          Malta          NA
## 23    Netherlands  37810.7727
## 24         Norway 113559.5000
## 25         Poland  45032.8636
## 26       Portugal   9348.6191
## 27        Romania          NA
## 28         Serbia          NA
## 29       Slovakia   2470.1173
## 30       Slovenia   2346.7668
## 31          Spain          NA
## 32         Sweden 207348.7273
## 33    Switzerland  74211.0864
## 34         Turkey          NA
## 35 United Kingdom  79609.8636</code></pre>
<p>As we can see, we provided 3 arguments to <code>aggregate.data.frame</code>:</p>
<ul>
<li><code>x</code> – the data we want to aggregate, in our case the <code>GrossSaving</code> column of the <code>gdi</code> data.frame</li>
<li><code>by</code> – a list of 1 element – <code>country</code> which specifies how the data will be grouped</li>
<li><code>FUN</code> – function which will be used, in our case arithmetic <code>mean</code></li>
</ul>
<div>
<img alt="Simple aggregate" src="https://i2.wp.com/jozefhajnala.gitlab.io/r/img/r003-01-aggregate.gif?w=456&ssl=1" data-recalc-dims="1">
<p>Simple aggregate</p>
</div>
<p>We can also see in our results, that for some countries such as Croatia, Cyprus and more, we have <code>NA</code> as a result. This is because numerical operations on vectors that contain even a single <code>NA</code> value will usually return <code>NA</code> as a result. If we want, we can usually work around this by providing an extra <code>na.rm = TRUE</code> argument to the function, which will strip the <code>NA</code> values before computation:</p>
<pre><code>aggregate.data.frame(x = gdi[&quot;GrossSaving&quot;]
                   , by = list(country = gdi[[&quot;country&quot;]])
                   , FUN = mean
                   , na.rm = TRUE
                   )</code></pre>
<pre><code>##           country  GrossSaving
## 1         Austria   24724.6227
## 2         Belgium   28961.7136
## 3        Bulgaria   -1711.6136
## 4         Croatia   18301.8727
## 5          Cyprus     438.6838
## 6  Czech Republic  208404.0000
## 7         Denmark   53667.7273
## 8         Estonia     487.1409
## 9           EU 28  924443.4983
## 10   Euro area 19  754148.9800
## 11        Finland    7656.7727
## 12         France  169311.6818
## 13        Germany  265215.6818
## 14         Greece    5289.8464
## 15        Hungary 1220273.5714
## 16        Iceland    -336.9933
## 17        Ireland    5831.3136
## 18          Italy  135086.8591
## 19         Latvia     147.1718
## 20      Lithuania     394.4595
## 21     Luxembourg    2510.5136
## 22          Malta          NaN
## 23    Netherlands   37810.7727
## 24         Norway  113559.5000
## 25         Poland   45032.8636
## 26       Portugal    9348.6191
## 27        Romania     271.2048
## 28         Serbia          NaN
## 29       Slovakia    2470.1173
## 30       Slovenia    2346.7668
## 31          Spain   57683.3333
## 32         Sweden  207348.7273
## 33    Switzerland   74211.0864
## 34         Turkey  129045.3843
## 35 United Kingdom   79609.8636</code></pre>
</div>
<div>
<h1>Grouping by more variables and small tweaks</h1>
<p>To make things even easier, we can use the fact that data.frames are also lists and we can therefore substitute <code>by = list(country = gdi[[&quot;country&quot;]]</code> by a much simpler and easier to read <code>gdi[&quot;country&quot;]</code>. Note and be careful that we only use <code>[]</code> for the sub-setting to get the sub-list, as <code>gdi[[&quot;country&quot;]]</code> would give us the vector of countries, as well as <code>gdi$country</code>:</p>
<pre><code>is.list(list(country = gdi[[&quot;country&quot;]]))</code></pre>
<pre><code>## [1] TRUE</code></pre>
<pre><code>is.list(gdi[&quot;country&quot;])</code></pre>
<pre><code>## [1] TRUE</code></pre>
<pre><code>is.list(gdi[[&quot;country&quot;]])</code></pre>
<pre><code>## [1] FALSE</code></pre>
<p>We can also group the data by more than one column, or a column translated in any way that fits our purposes, the only constraint is that the grouping elements (elements of the <code>by</code> argument), are each as long as the variables in the data frame <code>x</code>. And of course we also can aggregate more than 1 column at the same time.</p>
<p>As an example, let us</p>
<ul>
<li>calculate the mean not only for each country, but extend the grouping to decades</li>
<li>calculate the mean for more variables, not just <code>&quot;GrossSaving&quot;</code></li>
</ul>
<pre><code>aggregate.data.frame(x = gdi[c(&quot;ConspC&quot;
                             , &quot;AGDIpC&quot;
                             , &quot;GrossSaving&quot;
                             )]
                   , by = list(decade = paste0(substr(gdi[[&quot;time&quot;]], 1, 3), &quot;0s&quot;)
                             , country = gdi[[&quot;country&quot;]]
                             )
                   , FUN = mean
                   , na.rm = TRUE
                   )</code></pre>
<pre><code>##     decade        country      ConspC      AGDIpC   GrossSaving
## 1    1990s        Austria   19434.288   22578.640   20956.42000
## 2    2000s        Austria   21943.003   25145.214   25327.26000
## 3    2010s        Austria   23375.659   26135.279   26555.28571
## 4    1990s        Belgium   18938.482   21987.036   25395.72000
## 5    2000s        Belgium   21081.202   24088.858   30272.62000
## 6    2010s        Belgium   22594.490   24889.807   29636.12857
## 7    1990s       Bulgaria    3449.757    3494.050     -68.02000
## 8    2000s       Bulgaria    5578.549    5084.613   -2892.99000
## 9    2010s       Bulgaria    7813.849    7535.431   -1197.92857
## 10   1990s        Croatia         NaN         NaN           NaN
## 11   2000s        Croatia   51543.151   54675.474   15148.93750
## 12   2010s        Croatia   52515.373   57510.730   26709.70000
## 13   1990s         Cyprus   12302.576   12804.322     324.48600
## 14   2000s         Cyprus   15719.001   16671.523     727.46300
## 15   2010s         Cyprus   15788.383   15973.670      52.55000
## 16   1990s Czech Republic  159897.720  177018.694  132593.40000
## 17   2000s Czech Republic  200904.419  220895.554  204166.20000
## 18   2010s Czech Republic  228702.080  250919.280  268608.42857
## 19   1990s        Denmark  182933.412  180861.862   25988.60000
## 20   2000s        Denmark  205686.057  202040.782   48128.80000
## 21   2010s        Denmark  216901.757  220419.361   81351.28571
## 22   1990s        Estonia    3871.640    4249.542     270.28000
## 23   2000s        Estonia    6470.004    6500.569     160.78000
## 24   2010s        Estonia    7915.886    8509.247    1108.27143
## 25   1990s          EU 28   15214.920   16667.920  721765.79000
## 26   2000s          EU 28   17108.231   18635.367  890431.08600
## 27   2010s          EU 28   18125.639   19647.553 1001986.61714
## 28   1990s   Euro area 19   17607.510   19749.530  602634.05000
## 29   2000s   Euro area 19   19134.583   21366.280  733063.78800
## 30   2010s   Euro area 19   19740.273   21802.314  805915.67286
## 31   1990s        Finland   17220.232   18555.942    5659.60000
## 32   2000s        Finland   21616.329   23135.134    7536.40000
## 33   2010s        Finland   24631.860   26265.441    9255.28571
## 34   1990s         France   17903.622   20437.534  127231.80000
## 35   2000s         France   20696.734   23594.476  169890.90000
## 36   2010s         France   22088.164   25010.304  198541.28571
## 37   1990s        Germany         NaN   22112.486  215917.60000
## 38   2000s        Germany         NaN   23846.360  256128.00000
## 39   2010s        Germany         NaN   25848.440  313411.00000
## 40   1990s         Greece   12037.446   13388.490    9475.84600
## 41   2000s         Greece   15757.594   16707.839    8902.80000
## 42   2010s         Greece   13893.707   13620.999   -2861.51571
## 43   1990s        Hungary 1266767.368 1457751.030  868855.20000
## 44   2000s        Hungary 1737385.357 1838395.760 1140422.10000
## 45   2010s        Hungary 1727120.847 1864085.342 1646208.00000
## 46   1990s        Iceland         NaN         NaN           NaN
## 47   2000s        Iceland 3665145.798 3246304.470    5208.11000
## 48   2010s        Iceland 3491617.010 3112374.812  -11427.20000
## 49   1990s        Ireland   14151.552   14664.146    2801.42000
## 50   2000s        Ireland   20927.056   21749.417    6089.36000
## 51   2010s        Ireland   21803.019   22959.619    7626.88571
## 52   1990s          Italy   17703.632   20908.074  140031.20000
## 53   2000s          Italy   19631.544   22234.294  143875.11000
## 54   2010s          Italy   18584.590   20404.033  119000.54286
## 55   1990s         Latvia    3268.952    3188.684     -92.53200
## 56   2000s         Latvia    5400.014    5542.577     412.48900
## 57   2010s         Latvia    7088.467    6945.319     -60.63571
## 58   1990s      Lithuania    3260.052    3348.234     235.99800
## 59   2000s      Lithuania    5823.319    5947.266     422.12200
## 60   2010s      Lithuania    7934.150    8047.637     468.12857
## 61   1990s     Luxembourg   27550.836   31879.426    1411.74000
## 62   2000s     Luxembourg   32355.940   37663.168    2240.48000
## 63   2010s     Luxembourg   33700.054   40006.649    3681.11429
## 64   1990s          Malta         NaN         NaN           NaN
## 65   2000s          Malta         NaN         NaN           NaN
## 66   2010s          Malta         NaN         NaN           NaN
## 67   1990s    Netherlands   18829.144   20298.764   32126.40000
## 68   2000s    Netherlands   22457.564   23556.095   34825.10000
## 69   2010s    Netherlands   23204.377   24568.551   46136.28571
## 70   1990s         Norway  198946.604  207770.146   54321.60000
## 71   2000s         Norway  258438.853  269837.239   93655.20000
## 72   2010s         Norway  315219.324  334970.457  184307.00000
## 73   1990s         Poland   15919.330   18408.280   55897.00000
## 74   2000s         Poland   21828.780   22907.936   51046.50000
## 75   2010s         Poland   28529.733   28772.321   28681.85714
## 76   1990s       Portugal   10704.874   11856.422    8673.37200
## 77   2000s       Portugal   12562.477   13609.388   10208.75700
## 78   2010s       Portugal   12298.499   13066.471    8602.17000
## 79   1990s        Romania    8152.276    8427.888       5.88000
## 80   2000s        Romania   13854.047   13125.680  -11695.86000
## 81   2010s        Romania   19617.068   20486.043   20437.41667
## 82   1990s         Serbia         NaN         NaN           NaN
## 83   2000s         Serbia         NaN         NaN           NaN
## 84   2010s         Serbia         NaN         NaN           NaN
## 85   1990s       Slovakia    5050.218    5647.868    1810.30200
## 86   2000s       Slovakia    6824.102    7211.535    2189.37300
## 87   2010s       Slovakia    8479.726    8932.469    3342.47714
## 88   1990s       Slovenia    8573.522    9491.256    1009.57000
## 89   2000s       Slovenia   10719.190   12155.871    2626.34200
## 90   2010s       Slovenia   11666.361   12996.130    2902.51429
## 91   1990s          Spain   13961.670   15200.950   38715.00000
## 92   2000s          Spain   15808.068   17234.555   55932.60000
## 93   2010s          Spain   15180.146   16500.587   62894.14286
## 94   1990s         Sweden  182324.968  183408.674   68070.60000
## 95   2000s         Sweden  223273.148  229308.996  162984.10000
## 96   2010s         Sweden  251975.783  273511.021  370211.14286
## 97   1990s    Switzerland   41046.446   44855.838   53641.70000
## 98   2000s    Switzerland   44297.743   49556.481   69279.00000
## 99   2010s    Switzerland   47207.609   54372.377   95949.34286
## 100  1990s         Turkey         NaN         NaN           NaN
## 101  2000s         Turkey         NaN         NaN   69969.50000
## 102  2010s         Turkey         NaN         NaN  138891.36500
## 103  1990s United Kingdom   14625.190   15250.624   74342.00000
## 104  2000s United Kingdom   18919.282   19157.107   73031.80000
## 105  2010s United Kingdom   19716.279   20172.029   92769.85714</code></pre>
</div>
<div>
<h1>Using aggregate as a framework with custom aggregation functions</h1>
<p>Perhaps one of the most useful cases for <code>aggregate</code> is using it as a supporting framework for custom aggregations, since the <code>FUN</code> argument can be set to a function defined to suit specific purposes. This provides a very flexible environment where one can</p>
<ul>
<li>implement the custom aggregation functions in the most suitable way for the purpose</li>
<li>have unit testing for those functions</li>
<li>documentation and other aspects of implementation in place</li>
</ul>
<p>And use the aggregate as a reliable executor for such functionality, all using standard base R evaluation principles. An over-simplified example of the above approach could be the following:</p>
<p>We define the aggregation function <code>dummyaggfun</code></p>
<pre><code>dummyaggfun &lt;- function(v) {
  c(max = max(v)
  , min = min(v)
  , rng = max(v) - min(v)
  )
}</code></pre>
<p>And apply the aggregation</p>
<pre><code>aggregate.data.frame(gdi[&quot;GrossSaving&quot;]
                   , by = list(decade = paste0(substr(gdi[[&quot;time&quot;]], 1, 3), &quot;0s&quot;)
                             , country = gdi[[&quot;country&quot;]]
                             )
                   , FUN = dummyaggfun)</code></pre>
<pre><code>##     decade        country GrossSaving.max GrossSaving.min GrossSaving.rng
## 1    1990s        Austria        23226.80        19097.10         4129.70
## 2    2000s        Austria        31618.00        19897.90        11720.10
## 3    2010s        Austria        28755.60        25194.20         3561.40
## 4    1990s        Belgium        27350.10        24448.40         2901.70
## 5    2000s        Belgium        39041.60        25650.80        13390.80
## 6    2010s        Belgium        33126.40        27251.20         5875.20
## 7    1990s       Bulgaria          448.40         -483.00          931.40
## 8    2000s       Bulgaria         -758.60        -7200.60         6442.00
## 9    2010s       Bulgaria         2925.80        -4525.60         7451.40
## 10   1990s        Croatia              NA              NA              NA
## 11   2000s        Croatia              NA              NA              NA
## 12   2010s        Croatia              NA              NA              NA
## 13   1990s         Cyprus          545.50          185.62          359.88
## 14   2000s         Cyprus         1194.23          280.04          914.19
## 15   2010s         Cyprus              NA              NA              NA
## 16   1990s Czech Republic       145286.00       116646.00        28640.00
## 17   2000s Czech Republic       295156.00       156060.00       139096.00
## 18   2010s Czech Republic       293141.00       246605.00        46536.00
## 19   1990s        Denmark        42398.00         9694.00        32704.00
## 20   2000s        Denmark        72548.00        15456.00        57092.00
## 21   2010s        Denmark       111688.00        36971.00        74717.00
## 22   1990s        Estonia          401.20          200.20          201.00
## 23   2000s        Estonia         1115.10         -278.30         1393.40
## 24   2010s        Estonia         1415.10          839.50          575.60
## 25   1990s          EU 28              NA              NA              NA
## 26   2000s          EU 28      1077659.22       769059.51       308599.71
## 27   2010s          EU 28      1029579.38       976054.92        53524.46
## 28   1990s   Euro area 19              NA              NA              NA
## 29   2000s   Euro area 19       879005.73       596298.12       282707.61
## 30   2010s   Euro area 19       822350.15       781605.58        40744.57
## 31   1990s        Finland         6772.00         4436.00         2336.00
## 32   2000s        Finland        10986.00         6200.00         4786.00
## 33   2010s        Finland        10801.00         7534.00         3267.00
## 34   1990s         France       131350.00       119588.00        11762.00
## 35   2000s         France       206161.00       136627.00        69534.00
## 36   2010s         France       206511.00       191738.00        14773.00
## 37   1990s        Germany       217330.00       214836.00         2494.00
## 38   2000s        Germany       291363.00       216433.00        74930.00
## 39   2010s        Germany       345523.00       292290.00        53233.00
## 40   1990s         Greece        11398.81         8234.04         3164.77
## 41   2000s         Greece        11510.19         6390.06         5120.13
## 42   2010s         Greece         2897.71        -7727.10        10624.81
## 43   1990s        Hungary      1012178.00       710576.00       301602.00
## 44   2000s        Hungary      1614306.00       788873.00       825433.00
## 45   2010s        Hungary              NA              NA              NA
## 46   1990s        Iceland              NA              NA              NA
## 47   2000s        Iceland        88500.00       -52886.40       141386.40
## 48   2010s        Iceland              NA              NA              NA
## 49   1990s        Ireland         3219.60         2592.00          627.60
## 50   2000s        Ireland        11973.40         1384.10        10589.30
## 51   2010s        Ireland         9545.40         6374.60         3170.80
## 52   1990s          Italy       163452.00       116367.20        47084.80
## 53   2000s          Italy       156700.60       111087.70        45612.90
## 54   2010s          Italy       124778.90       104720.00        20058.90
## 55   1990s         Latvia           36.17         -206.97          243.14
## 56   2000s         Latvia         1922.58          -81.56         2004.14
## 57   2010s         Latvia          620.12         -555.45         1175.57
## 58   1990s      Lithuania          610.73          -78.62          689.35
## 59   2000s      Lithuania         1003.24         -719.82         1723.06
## 60   2010s      Lithuania         1516.18         -119.85         1636.03
## 61   1990s     Luxembourg         1488.10         1344.60          143.50
## 62   2000s     Luxembourg         2964.30         1584.80         1379.50
## 63   2010s     Luxembourg         4119.00         3192.70          926.30
## 64   1990s          Malta              NA              NA              NA
## 65   2000s          Malta              NA              NA              NA
## 66   2010s          Malta              NA              NA              NA
## 67   1990s    Netherlands        34110.00        28988.00         5122.00
## 68   2000s    Netherlands        47342.00        28712.00        18630.00
## 69   2010s    Netherlands        50314.00        40945.00         9369.00
## 70   1990s         Norway        66426.00        42704.00        23722.00
## 71   2000s         Norway       140538.00        51542.00        88996.00
## 72   2010s         Norway       253022.00       117285.00       135737.00
## 73   1990s         Poland        69410.00        43081.00        26329.00
## 74   2000s         Poland        84850.00        27414.00        57436.00
## 75   2010s         Poland        49574.00        14823.00        34751.00
## 76   1990s       Portugal         9717.60         7907.00         1810.60
## 77   2000s       Portugal        13217.79         8530.86         4686.93
## 78   2010s       Portugal        11929.76         6245.18         5684.58
## 79   1990s        Romania         2749.90        -2122.40         4872.30
## 80   2000s        Romania         1146.30       -24932.80        26079.10
## 81   2010s        Romania              NA              NA              NA
## 82   1990s         Serbia              NA              NA              NA
## 83   2000s         Serbia              NA              NA              NA
## 84   2010s         Serbia              NA              NA              NA
## 85   1990s       Slovakia         2073.54         1132.87          940.67
## 86   2000s       Slovakia         3119.07         1697.54         1421.53
## 87   2010s       Slovakia         4622.03         2627.62         1994.41
## 88   1990s       Slovenia         1214.62          731.94          482.68
## 89   2000s       Slovenia         3578.14         1587.45         1990.69
## 90   2010s       Slovenia         3175.60         2337.12          838.48
## 91   1990s          Spain              NA              NA              NA
## 92   2000s          Spain        93604.00        38368.00        55236.00
## 93   2010s          Spain        74681.00        53982.00        20699.00
## 94   1990s         Sweden       100539.00        50227.00        50312.00
## 95   2000s         Sweden       257867.00        85342.00       172525.00
## 96   2010s         Sweden       452834.00       280354.00       172480.00
## 97   1990s    Switzerland        56395.80        51875.60         4520.20
## 98   2000s    Switzerland        83474.60        59724.20        23750.40
## 99   2010s    Switzerland       104819.20        84475.00        20344.20
## 100  1990s         Turkey              NA              NA              NA
## 101  2000s         Turkey              NA              NA              NA
## 102  2010s         Turkey              NA              NA              NA
## 103  1990s United Kingdom        84296.00        55971.00        28325.00
## 104  2000s United Kingdom       102670.00        58101.00        44569.00
## 105  2010s United Kingdom       126386.00        68648.00        57738.00</code></pre>
</div>
<div>
<h1>Advanced details of aggregate use</h1>
<p>Examining the code of <code>aggregate.data.frame</code> will give us a good picture of how the function operates. This could be roughly described in the following way, abstracting from the defensive programming aspects and details and focusing on the functionality itself:</p>
<ol>
<li>create <code>grp</code> – group labels that are (most likely) numbers stored as <code>character</code> by factorizing the elements of <code>by</code></li>
<li>create <code>y</code> – a <code>data.frame</code> with the data grouping resulting from processing <code>by</code>, to which the results will be binded</li>
<li>take the input data <code>x</code> (coerced to a <code>data.frame</code>) and column by column <code>split</code> the data into groups according to <code>grp</code></li>
<li>apply <code>FUN</code> (that was retrieved by <code>match.fun</code>) on the results of the <code>split</code>, assign the results into <code>z</code></li>
<li>bind the <code>y</code> that has the group labels with <code>z</code> that has the results</li>
</ol>
<blockquote>
<h4>Providing the <code>FUN</code> argument</h4>
<p>One specific should be noted – providing <code>FUN</code> as a character string (name of the function, e.g. <code>FUN = &quot;mean&quot;</code>) will trigger the non-standard evaluation part of code in <code>match.fun</code>, which we may like to avoid. This is easily achieved by providing the <code>FUN</code> argument with the function diretly, not via the function’s name (e.g. <code>FUN = mean</code>) as in that case <code>match.fun</code> just returns the provided <code>FUN</code> without further changes</p>
</blockquote>
<blockquote>
<h4>Argument structure of <code>FUN</code></h4>
<p>The value returned from <code>split</code> is a list of vectors containing the values for the groups. The <code>FUN</code> is provided with the elements of that list via <code>lapply</code>, which are vectors. This is helpful for the setup of the custom <code>FUN</code>. We can also take advantage of the <code>...</code> concept and dedicate a part of the <code>FUN</code> code to process more provided arguments.</p>
</blockquote>
</div>
<div>
<h1>Aggregate’s methods for other object classes</h1>
<p>So far we have mostly used the <code>aggregate.data.frame</code> method, however <code>aggregate</code> is a generic function with methods for multiple classes of objects, here is a very quick overview:</p>
<ul>
<li><code>aggregate.default</code> – the default method, which uses the time series method if <code>x</code> is a time series, and otherwise coerces <code>x</code> to a <code>data.frame</code> and calls the <code>data.frame</code> method</li>
<li><code>aggregate.ts</code> – the time series method, is further discussed in R’s help on <code>?aggregate</code>. Investigation of the code is also very advisable.</li>
<li><code>aggregate.formula</code> – the formula method, is a standard formula interface to <code>aggregate.data.frame</code></li>
<li><code>aggregate.data.frame</code> – is discussed in this article</li>
</ul>
</div>
<div>
<h1>Alternatives to base R</h1>
<ul>
<li><a rel="nofollow" href="http://www.milanor.net/blog/aggregation-dplyr-summarise-summarise_each/" target="_blank">dplyr::summarize</a> and friends</li>
<li>using <a rel="nofollow" href="https://datascienceplus.com/efficient-aggregation-using-data-table/" target="_blank">data.table</a></li>
</ul>
</div>
<div>
<h1>TL;DR – Just want the code</h1>
<blockquote>
<p>No time for reading? <a rel="nofollow" href="https://jozefhajnala.gitlab.io/r/post/data/r003-aggregation.r" target="_blank">Click here to get just the code with commentary</a></p>
</blockquote>
</div>
<div>
<h1>Exercises</h1>
<ol>
<li>Looking at the <code>aggregate(state.x77, list(Region = state.region), mean)</code> example in <code>?aggregate</code>, how does R know how to match the states to the regions? Would the example still work if the data in <code>state.x77</code> were sorted differently?</li>
<li>What is the difference between <code>aggregate(x = gdi[&quot;GrossSaving&quot;], by = gdi[&quot;country&quot;], FUN = mean)</code> and <code>aggregate(x = gdi[[&quot;GrossSaving&quot;]], by = gdi[&quot;country&quot;], FUN = mean)</code>. What is the issue with the latter? Looking at the code of <code>aggregate.data.frame</code>, why does the latter still work?</li>
</ol>
</div>
<div>
<h1>References</h1>
<ol>
<li><a rel="nofollow" href="https://www.rdocumentation.org/packages/stats/versions/3.5.0/topics/aggregate?tap_a=5644-dce66f&tap_s=10907-287229" target="_blank">aggregate</a> at rdocumentation.org</li>
<li><a rel="nofollow" href="https://www.rdocumentation.org/packages/base/versions/3.5.0/topics/split?tap_a=5644-dce66f&tap_s=10907-287229" target="_blank">split</a> at rdocumentation.org</li>
<li><a rel="nofollow" href="https://stackoverflow.com/questions/3057341/how-to-use-rs-ellipsis-feature-when-writing-your-own-function" target="_blank">discussion on <code>...</code> (ellipsis)</a> on stack overflow</li>
</ol>
</div>
<div>
<h1>Exercise answers</h1>
<p><a rel="nofollow" href="https://jozefhajnala.gitlab.io/r/post/data/r003-aggregation.r" target="_blank">At the bottom of the code for the article</a></p>
<h2>Did you find the article helpful or interesting? Help others find it by sharing</h2>
<div></div>
</div>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://jozefhajnala.gitlab.io/r/r003-aggregation/"> Jozef's Rblog</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/hXz69QoqLVQ" height="1"></a></div>