Calculate By Group in One Column And Grand Total By Another

Hello All, thanks as always for stopping by.

So I have a dataframe here with the following structure, when I run the command


So I am seeking to create a tibble that basically tells how many rows there are when grouped by member_type, and month (taken from the columns member_type, and started_at). However, I want to then see what each row's count is in terms of percent of the total of BOTH groups, not just the total of its own group.

So, to first get the count for each row by member_type and month, I do the following code:

row_member_breakdown <- df %>%
group_by(member_type, format(started_at,"%m")) %>%
summarise(ride_count = n())

print(row_member_breakdown, n=24)

I get this output:

Next, I try to add the percentage column:

row_member_breakdown <- row_member_breakdown %>%
+ mutate(percent_of_total = (ride_count * 100)/sum(ride_count))
> print(row_member_breakdown, n = 24)

I get the following output:

It's clear that the percentages are only being calculated relative to group when I want them calculated relative to the grand total. How would I change my code to do this?

Thanks a ton!

Have you tried ungroup() ?

Yes it did! Cool now I know ungroup. Thanks a ton! I'm new, but wrapped up my first project, so I'm sure I'll use more ungroup soon.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.