Get the proportion of events by group, given the condition of another column

I'd like to know the proportion of events by site (i.e., by group) where "y" occurred at least once.

Example of data:

site event occurrence 
A    A01   y          
A    A01   n          
A    A02   n          
A    A02   n          
A    A03   y          
B    B01   y          
B    B01   n          
B    B02   n          
B    B02   n          

Desired outcome:

site event occurrence proporition
A    A01   y          0.666
A    A01   n          0.666
A    A02   n          0.333
A    A02   n          0.333
A    A03   y          0.666
B    B01   y          0.5
B    B01   n          0.5
B    B02   n          0.5
B    B02   n          0.5

So, at site "A", a the proportion of unique events with at least one occurrence of "Y" was 0.666. At site "B", a the proportion of unique events with at least one occurrence of "Y" was 0.5.

Thank you in advance!



Read more here: https://stackoverflow.com/questions/66995882/get-the-proportion-of-events-by-group-given-the-condition-of-another-column

Content Attribution

This content was originally published by Kate at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: