Calculating ratios with Tidyverse

Explaining summarise hidden behaviour

Calculating percentages is a fairly common operation, right? However, doing it without leaving the pipeflow always force me to do some bizarre piping such as double grouping and summarise.

I am using again the nuclear accidents dataset, and trying to calculate the percentage of accidents that happened in Europe each year.

DateLocationCost (millions 2013US$)INESSmyth MagnitudeRegionFatalitiesYearIn_Europe
3/11/2011Fukushima Prefecture, Japan166089.077.5A5732011FALSE
12/8/1995Tsuruga, Japan15500.0NANAA01995FALSE
12/19/1989Vandellòs, Spain930.93NAWE01989TRUE
2/1/2010Vernon, Vermont, United States808.9NANANA02010FALSE

This can be achieved by several ways. One common path would be:


Another one more bizarre would be totalizing first, then grouping including that amount (to avoid being dropped) and then summarise.


Kind of weird. However, there is a much simpler way:


The first time I saw this result I didn’t understand it because if you have your dataframe grouped by Year and In_Europe then sum(N) should be equal to N. What is going on? This behaviour has to do with a tricky funcionality of summarise.

Take a closer look of the grouping variables at the console output. Before the summarise function the dataframe seems grouped normally and the operation will be performed within each group:

However, once the dataframe is summarized, the resulting dataframe is no longer grouped by the same original variables:

Actually, the default behaviour of summarise is to drop the last group. The reason behind that is that, once the operation is performed you should have only one obervation per group, and it has no sense to grouping by it anymore. That’s why the last example I show above works. Now you can take advantage of it too!

Furthermore, you can learn more about the dplyr 1.0.0 last minute additions which include an explicit message to highlight the behaviour we have talked about here.

Pablo Cánovas
Pablo Cánovas
Data Scientist at Repsol

Data Scientist, formerly physicist | Tidyverse believer, piping life | Hanging out at TypeThePipe