Computing Conditional Sums and Conditional Means in R
Overview
If we’re talking about Excel, sometimes we need functions like sumif()
or averageif()
. In R, while there’s no such simple function, the apply family of functions serves as a powerful superior alternative.
It’s beneficial to meticulously learn these functions, but for the time being, let’s just focus on calculating conditional sums and averages.
Example
Let’s load the iris dataset.
If we randomly look at the 10th, 50th, 90th, and 130th data points, we can see that the species have been categorized as a categorical variable. To calculate the sum and average of Sepal.Length
for each species, we can use the tapply()
function.
As shown in the example above, the first argument is the data to be calculated, the second is the classification criterion, and the third is the sum or average.