Calculating ratios with Tidyverse

May 12, 2020

[This article was first published on R | TypeThePipe, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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.

Date Location Cost (millions 2013US$) INES Smyth Magnitude Region Fatalities Year In_Europe
3/11/2011 Fukushima Prefecture, Japan 166089.0 7 7.5 A 573 2011 FALSE
12/8/1995 Tsuruga, Japan 15500.0 NA NA A 0 1995 FALSE
12/19/1989 Vandellòs, Spain 930.9 3 NA WE 0 1989 TRUE
2/1/2010 Vernon, Vermont, United States 808.9 NA NA NA 0 2010 FALSE

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

Year In_Europe N Total_per_year Ratio
1989 FALSE 4 6 0.67
1989 TRUE 2 6 0.33
1990 FALSE 1 1 1.00
1991 FALSE 3 3 1.00

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

Year In_Europe Total_per_year N Ratio
1989 FALSE 6 4 0.67
1989 TRUE 6 2 0.33
1990 FALSE 1 1 1.00
1991 FALSE 3 3 1.00

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

Year In_Europe N Ratio
1989 FALSE 4 0.67
1989 TRUE 2 0.33
1990 FALSE 1 1.00
1991 FALSE 3 1.00

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.

To leave a comment for the author, please follow the link and comment on their blog: R | TypeThePipe. offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)