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

Summarise and calculate percentage complete by group in R

I have a dataframe that looks like this:

example <- data.frame(
  sub = c(rep(1091, 3),
          rep(2091,4)),
  completed_sessions = c(1,0,1,0,1,1,1)
)

I want to get the percent_complete by sub, with it calculated by num_complete/total_entries. num_complete is the count of ‘1’s per sub, total_entries is the total number of observations of PID. How can I do this?

This is the desired output:

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

example_solution <- data.frame(
  sub = c(1091, 2091),
  num_complete = c(2,3),
  total_entries = c(3,4),
  percent_complete = c(66.67, 75.00)
)

Thank you!

>Solution :

example %>%
   summarise(num_complete = sum(completed_sessions), 
             total_entries = n(),
             percent_complete = num_complete/total_entries*100,
             .by = sub)

   sub num_complete total_entries percent_complete
1 1091            2             3         66.66667
2 2091            3             4         75.00000
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