Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

R: Count percentage of "yes" in a column by group

I want to count the percentage of the response "yes" in a column that contains "yes" and "no".

Student Response
S1 yes
S2 yes
S1 no
S5 yes
S5 yes
S7 no
S8 no

This is what I would like to get

Student Response percentage
S1 yes 50%
S2 yes 100%
S1 no 50%
S5 yes 100%
S5 yes 100%
S7 no 0%
S8 no 0%

This is what I have been working but I don’t understand what’s not working. Thanks!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

df %>%
group_by(Student)%>%
summarize(sum_total = n(Response)%>%
filter(Response== "yes") %>%
summarize(sum_yes = n(Response))%>%
mutate(yes_percentage = scales::label_percent()(sum_yes/sum_total))

>Solution :

You can do this using the base function ave

dat$percentage <- scales::label_percent()(ave( dat$Response=="yes", dat$Student, FUN=mean))

dat
  Student Response percentage
1      S1      yes        50%
2      S2      yes       100%
3      S1       no        50%
4      S5      yes       100%
5      S5      yes       100%
6      S7       no         0%
7      S8       no         0%
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading