R Tutorial: Count With Your Data

Learn how to count rows and groups in R using dplyr's count() function on Great British Bake Off data. The tutorial covers counting distinct values, counting by multiple variables, calculating proportions with mutate(), and why ungroup() matters before dividing. A 95-baker dataset shows how count() beats manual group_by() plus summarize() for clarity and correctness.
Chapters
Now that we've gotten to know our data better, it's time to do some careful counting. While Glimpse and SCIM give nice overviews, counting values within variables will help you to better understand the underlying structure of your data and can help guide you toward asking good questions about your data. We'll work with the baker's data again, but this time we'll use the full data for all bakers who have appeared on the Great British Bake Off. Here is a glimpse of bakers. We have 95 observations or bakers and 10 variables. Let's start with a simple how many series of the TV show do we have data for? More specifically, how many distinct series are there? For that, we can use the distinct function from the dplyr package and we see that there are eight. So we have eight series and we know that each row is a baker. How can we count the number of distinct bakers per series? We'll use a new dplyr verb count. The argument to count is a variable or variables. To group by count adds a new column named in to store the counts. Since each row is a baker, we know that most series feature a dozen bakers. Series one had only 10, and series four had a baker's dozen or 13. Using count gives you the same output as using groupby followed by summarize. To count the rows by group, we can also count by more than one variable. Here we count by two variables, airedus and series. We can see that series 4, 5, 6, and 7 have aired in the U.S. the in column is still the number of bakers count helpfully. Does an extra ungrouped step for you? Let's say that after counting, we add a mutate to make a new column with the proportion of bakers in each series for the whole show. If we use count before mutate, our proportion is right. There are 95 bakers total and 10.5% appeared in series one. But if we instead used group by then summarize, the result might be surprising. This is because our tibble is still grouped, which you can see on the right. To get the answer we are after here, we would need to add an ungroup before the mutate. This ensures that the denominator is as expected. Comparing the code on the left and the right, they both do the same thing, but the left is more straightforward and easier to read. What if we want to know how many series have aired in the US to answer this question, we don't care about the number of bakers in the far right column. We only care about the first two columns here to just count the number of series that aired in the us, Ignoring the number of bakers. We can count by the same variable twice to roll up a level. The output here has only two rows, one for when airedus is false and one for when it's true. The column named NN is the number of series that erred in the US and the number that did not. This second count ignores the values in the n column that we see on the left. Now we know that there has been an even split. So far, US viewers have seen only half of the series aired in the uk. Now it's time to practice your counting skills with our Bake off data.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







