<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/how-to-aggregate-data-in-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://www.displayr.com/learn-how-aggregate-data-in-r/?utm_medium=Feed&utm_source=Syndication"> R – Displayr</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>The process involves two stages. First, collate individual cases of raw data together with a grouping variable. Second, perform which calculation you want on each group of cases. These two stages are wrapped into a single function.</p>
<p>To perform aggregation, we need to specify three things in the code:</p>
<ul>
<li>The data that we want to aggregate</li>
<li>The variable to group by within the data</li>
<li>The calculation to apply to the groups (what you want to find out)</li>
</ul>
<h2>Example data</h2>
<p>The raw data shown below consists of one row per case. Each case is an employee at a restaurant.</p>
<p><iframe width="450" src="https://embedded.azureedge.net/75668/282697/805ac406-78d1-4f6a-8427-21190ca947eb.html?v=31738-acbf0e4aa7-1630713992"></iframe></p>
<p>Load the example data by running the following R code:</p>
<pre> 
library(flipAPI)
data = DownloadXLSX(&quot;https://wiki.q-researchsoftware.com/images/1/1b/Aggregation_data.xlsx&quot;, want.row.names = FALSE, want.data.frame = TRUE)
</pre>
<p>Perform aggregation with the following R code.</p>
<pre>
agg = aggregate(data,
                by = list(data$Role),
                FUN = mean)
</pre>
<p>This produces a table of the average salary and age by role, as below.<br>
<iframe src="https://embedded.azureedge.net/75668/282697/7844d341-c37a-4b59-a0a9-de3705509ef4.html?v=31738-acbf0e4aa7-1943210984" width="341" height="141"></iframe></p>
<h2>The aggregate function</h2>
<p>The first argument to the function is usually a data.frame.</p>
<p>The <em>by</em> argument is a list of variables to group by. This must be a list even if there is only one variable, as in the example.</p>
<p>The <em>FUN</em> argument is the function which is applied to all columns (i.e., variables) in the grouped data. Because we cannot calculate the average of categorical variables such as <em>Name</em> and <em>Shif</em>t, they result in empty columns, which I have removed for clarity.</p>
<h2>Other aggregation functions</h2>
<p>Any function that can be applied to a numeric variable can be used within <em>aggregate</em>. Maximum, minimum, count, standard deviation and sum are all popular.</p>
<p>For more specific purposes, it is also possible to write your own function in R and refer to that within <em>aggregate</em>. I’ve demonstrated this below where the second largest value of each group is returned, or the largest if the group has only one case. Note also that the groups are formed by <em>Role</em> and by <em>Shift</em> together.</p>
<pre>
second = function(x) {
            if (length(x) == 1)
                return(x)
            return(sort(x, decreasing = TRUE)[2])}

agg = aggregate(data,
                by = list(data$Role, data$Shift),
                FUN = second)
</pre>
<p><iframe src="https://embedded.azureedge.net/75668/282697/67a57a67-1148-4859-af3a-edf347b6a720.html?v=31738-acbf0e4aa7373142161" width="441" height="201"></iframe></p>
<h2>Additional features</h2>
<p>The <em>aggregate</em> function has a few more features to be aware of:</p>
<ul>
<li>Grouping variable(s) and variables to be aggregated can be specified with R’s formula notation.</li>
<li>Setting <em>drop = TRUE</em> means that any groups with zero count are removed.</li>
<li><em>na.action</em> controls the treatment of missing values within the data.</li>
</ul>
<hr>
<p><span>TRY IT OUT</span><br>
The analysis in this post was performed in <a rel="nofollow" href="http://www.displayr.com/" target="_blank">Displayr</a> using R. You can <a rel="nofollow" href="https://app.displayr.com/Try/Aggregating%20Data" target="_blank">repeat or amend this analysis</a> for yourself in Displayr.</p>
<hr>
<p> </p>
	<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://www.displayr.com/learn-how-aggregate-data-in-r/?utm_medium=Feed&utm_source=Syndication"> R – Displayr</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/l8NfXmiJgoI" height="1"></a></div>