GaB
GaB

Reputation: 1134

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

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

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.

Upvotes: 1

Views: 62

Answers (1)

Martin Gal
Martin Gal

Reputation: 16998

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.

Upvotes: 1

Related Questions