How to get Percentages with reference to one number in my table which is 100 % in R, with tidyverse?

Viewed 42

I am trying to get Percentages, in R, with tidyverse:

  • based on the first row NSTEMI which regardless of its counts (compared to the following categories) is always 100 %.
  • Yet I want my percentages 'Seen by Cardiologist', 'Admitted to Cardiac Unit or Ward', 'Eligible for Angiography' to be calculated with reference to NSTEMI. In lay language will sound like - out of NSTEMI how much percentage have been seen by the cardiologist, admitted to cardiac unit and are eligible for angiography;
  • Yet, the last two rows 'Underwent Angiography' and 'Underwent Angiography Before Discharge' and their Percentages to be calculated with reference to the 'Eligible for Angiography' which is my fourth row. It is like from 'Eligible for Angiography' how many 'Underwent Angiography' and 'Underwent angiography before discharge'

This is my sample of data :

structure(list(Category = c("NSTEMI", "Seen by cardiologist", 
"Admitted to cardiac unit or Ward", "Eligible for angiography", 
"Underwent angiography", "Underwent angiography before discharge"
), Counts = c(196L, 196L, 158L, 174L, 174L, 173L)), row.names = c(NA, 
-6L), class = c("tbl_df", "tbl", "data.frame"))

An example of the table I want looks as bellow picture:

enter image description here

As observed, the table I want is done based on my principles designed above.

1 Answers

This special task isn't a real show case for tidyverse. But you could use:

library(dplyr)

df %>% 
  group_by(grp = c(rep(1,4), rep(2,2))) %>% 
  mutate(Frequency  = Counts/first(Counts),
         Percentage = paste0(round(Frequency * 100, 2), " %")) %>% 
  ungroup() %>% 
  select(-grp)

which returns

# A tibble: 6 x 4
  Category                               Counts Frequency Percentage
  <chr>                                   <int>     <dbl> <chr>     
1 NSTEMI                                    196     1     100 %     
2 Seen by cardiologist                      196     1     100 %     
3 Admitted to cardiac unit or Ward          158     0.806 80.61 %   
4 Eligible for angiography                  174     0.888 88.78 %   
5 Underwent angiography                     174     1     100 %     
6 Underwent angiography before discharge    173     0.994 99.43 %   

This solution is based on the position of your Categorys, if necessary you could use a string comparison.

I created a grouping variable: the first four rows of your data belong to group 1, the last two into group 2. c(rep(1,4), rep(2,2)) creates that grouping: 1 1 1 1 2 2, 1 is repeated four times, 2 twice. The first-function takes the first element of a group. Thanks to the grouping the first element of group 1 is taken from row NSTEMI, the first element of group 2 from row Underwent angiography.

Related