I’m writing some code where I need to check whether all group sizes for a given input of data are equal. For example, suppose I wanted to know whether the “mpg” dataset (in the ggplot2 package) has:
- Equal numbers of cars for every manufacturer
- Equal numbers of cars for each type of drive (4-wheel, front-wheel, rear-wheel)
- Equal numbers of cars for each engine type (4-cylinder, 6-cylinder, 8-cylinder)
For data like mpg, some of those questions can be answered by inspecting the summary output
library(ggplot2) # contains the mpg dataset
summary(mpg$drive) # shows the breakdown of cars by drive-type,
# which we can verify is unequal
But I feel like I’m missing an easy way to check whether group sizes are equal. Is there some single, mythical function I can call like are.groups.of.equal.size(x)? Or another base function (or composition of them) that would return such information?
As Joran said we could invent 100s of ways from here till Christmas on how to do this one. I smell a microbenchmark challenge: