0

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:

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!

jo_
  • 677
  • 2
  • 11
  • Does this answer your question? [Summarizing by subgroup percentage in R](https://stackoverflow.com/questions/27134516/summarizing-by-subgroup-percentage-in-r) – LMc Jul 27 '23 at 18:15

1 Answers1

0
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
Onyambu
  • 67,392
  • 3
  • 24
  • 53