Hello All, thanks as always for stopping by.
So I have a dataframe here with the following structure, when I run the command
str(df)
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!