Ahmed Mabrouk
Ahmed Mabrouk

Reputation: 39

A Nice table for combined count and percentage for more than one categorical variable

I am using R markdown and qwraps2 and dplyr packages

library(dplyr)
library(qrwaps2)

I have a dataset with more than one categorical variable and few numeric variables that I want to get the count and percentage of the categorical variables in the following format: count (percentage %)

A sample of the dataset is below:

structure(list(SIDE = c("Left", "Right", "Left", "Right", "Left", 
"Right", "Right", "Right", "Right", "Right", "Left", "Left", 
"Left", "Right", "Left", "Right", "Right", "Left", "Left", "Left", 
"Left", "Right", "Right"), PREOP_mTFA = c(163.5, 164.9, 168.7, 
170.3, 162.8, 166.7, 171, 165.9, 165.9, 170.8, 170.5, 173.3, 
167.7, 170.7, 159, 170.9, 168.2, 171.2, 164, 166.6, 169.1, 171.2, 
175.9), PREOP_mLDFA = c(86, 95, 90, 86, 92, 89, 92, 96, 90, 86, 
89, 87, 93, 90, 98, 89, 90, 88, 92, 91, 89, 90, 88), KL = c("ONE", 
"THREE", "TWO", "FOUR", "FOUR", "FOUR", "THREE", "TWO", "ONE", 
"ONE", "TWO", "TWO", "TWO", "THREE", "ONE", "FOUR", "TWO", "THREE", 
"THREE", "TWO", "ONE", "TWO", "TWO"), WEDGE = c("OWHTO", "CWHTO", 
"OWHTO", "CWHTO", "OWDFO", "OWDFO", "CWDFO", "CWDFO", "CWDFO", 
"OWDFO", "OWHTO", "MOWHTO", "LCWHTO", "LCWHTO", "LCWHTO", "LCWHTO", 
"MOWHTO", "LCWDFO", "MOWDFO", "MCWDFO", "MCWDFO", "LOWDFO", "LOWDFO"
)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-23L))

I used the following code to get count and percentage for each categorical variable but they come as separate.

DLO_TEST %>% group_by(SIDE) %>% 
  summarise(count = n())
DLO_TEST %>% group_by(SIDE) %>% 
  summarise(percent = 100 * n() / nrow(DLO_TEST))

DLO_TEST %>% group_by(KL) %>% 
  summarise(count = n())
DLO_TEST %>% group_by(KL) %>% 
  summarise(percent = 100 * n() / nrow(DLO_TEST))

DLO_TEST %>% group_by(WEDGE) %>% 
  summarise(count = n())
DLO_TEST %>% group_by(WEDGE) %>% 
  summarise(percent = 100 * n() / nrow(DLO_TEST))

I want to have one output in the format of count then percentage in parentheses with percentage sign% : count (percentage %). And in a nice table. How to modify the code to do that? Or any suggestions?

Upvotes: 0

Views: 28

Answers (1)

Peter
Peter

Reputation: 7790

library(dplyr)
library(magrittr)
library(qwraps2)
options(qwraps2_markup = "markdown")
packageVersion("qwraps2")
#> [1] '0.6.0'
DLO_TEST <-
  structure(list(SIDE = c("Left", "Right", "Left", "Right", "Left", "Right", "Right", "Right", "Right", "Right", "Left", "Left", "Left", "Right", "Left", "Right", "Right", "Left", "Left", "Left", "Left", "Right", "Right"), PREOP_mTFA = c(163.5, 164.9, 168.7, 170.3, 162.8, 166.7, 171, 165.9, 165.9, 170.8, 170.5, 173.3, 167.7, 170.7, 159, 170.9, 168.2, 171.2, 164, 166.6, 169.1, 171.2, 175.9), PREOP_mLDFA = c(86, 95, 90, 86, 92, 89, 92, 96, 90, 86, 89, 87, 93, 90, 98, 89, 90, 88, 92, 91, 89, 90, 88), KL = c("ONE", "THREE", "TWO", "FOUR", "FOUR", "FOUR", "THREE", "TWO", "ONE", "ONE", "TWO", "TWO", "TWO", "THREE", "ONE", "FOUR", "TWO", "THREE", "THREE", "TWO", "ONE", "TWO", "TWO"), WEDGE = c("OWHTO", "CWHTO", "OWHTO", "CWHTO", "OWDFO", "OWDFO", "CWDFO", "CWDFO", "CWDFO", "OWDFO", "OWHTO", "MOWHTO", "LCWHTO", "LCWHTO", "LCWHTO", "LCWHTO", "MOWHTO", "LCWDFO", "MOWDFO", "MCWDFO", "MCWDFO", "LOWDFO", "LOWDFO")), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -23L))

The default settings for summary_table are close to what you are looking for:

summary_table(DLO_TEST)
#> 
#> 
#> |                          |DLO_TEST (N = 23)       |
#> |:-------------------------|:-----------------------|
#> |**SIDE**                  |&nbsp;&nbsp;            |
#> |&nbsp;&nbsp; Left         |11 (48)                 |
#> |&nbsp;&nbsp; Right        |12 (52)                 |
#> |**PREOP_mTFA**            |&nbsp;&nbsp;            |
#> |&nbsp;&nbsp; minimum      |159.00                  |
#> |&nbsp;&nbsp; median (IQR) |168.70 (165.90, 170.85) |
#> |&nbsp;&nbsp; mean (sd)    |168.21 &plusmn; 3.83    |
#> |&nbsp;&nbsp; maximum      |175.90                  |
#> |**PREOP_mLDFA**           |&nbsp;&nbsp;            |
#> |&nbsp;&nbsp; minimum      |86.00                   |
#> |&nbsp;&nbsp; median (IQR) |90.00 (88.50, 92.00)    |
#> |&nbsp;&nbsp; mean (sd)    |90.26 &plusmn; 3.12     |
#> |&nbsp;&nbsp; maximum      |98.00                   |
#> |**KL**                    |&nbsp;&nbsp;            |
#> |&nbsp;&nbsp; FOUR         |4 (17)                  |
#> |&nbsp;&nbsp; ONE          |5 (22)                  |
#> |&nbsp;&nbsp; THREE        |5 (22)                  |
#> |&nbsp;&nbsp; TWO          |9 (39)                  |
#> |**WEDGE**                 |&nbsp;&nbsp;            |
#> |&nbsp;&nbsp; CWDFO        |3 (13)                  |
#> |&nbsp;&nbsp; CWHTO        |2 (9)                   |
#> |&nbsp;&nbsp; LCWDFO       |1 (4)                   |
#> |&nbsp;&nbsp; LCWHTO       |4 (17)                  |
#> |&nbsp;&nbsp; LOWDFO       |2 (9)                   |
#> |&nbsp;&nbsp; MCWDFO       |2 (9)                   |
#> |&nbsp;&nbsp; MOWDFO       |1 (4)                   |
#> |&nbsp;&nbsp; MOWHTO       |2 (9)                   |
#> |&nbsp;&nbsp; OWDFO        |3 (13)                  |
#> |&nbsp;&nbsp; OWHTO        |3 (13)                  |

The above is generated by calling qsummary on DLO_TEST. To get the % symbol you’ll need to change the default for qsummary

s <- qsummary(DLO_TEST, n_perc_args = list(show_symbol = TRUE))
summary_table(DLO_TEST, summaries = s)
#> 
#> 
#> |                          |DLO_TEST (N = 23)       |
#> |:-------------------------|:-----------------------|
#> |**SIDE**                  |&nbsp;&nbsp;            |
#> |&nbsp;&nbsp; Left         |11 (47.83%)             |
#> |&nbsp;&nbsp; Right        |12 (52.17%)             |
#> |**PREOP_mTFA**            |&nbsp;&nbsp;            |
#> |&nbsp;&nbsp; minimum      |159.00                  |
#> |&nbsp;&nbsp; median (IQR) |168.70 (165.90, 170.85) |
#> |&nbsp;&nbsp; mean (sd)    |168.21 &plusmn; 3.83    |
#> |&nbsp;&nbsp; maximum      |175.90                  |
#> |**PREOP_mLDFA**           |&nbsp;&nbsp;            |
#> |&nbsp;&nbsp; minimum      |86.00                   |
#> |&nbsp;&nbsp; median (IQR) |90.00 (88.50, 92.00)    |
#> |&nbsp;&nbsp; mean (sd)    |90.26 &plusmn; 3.12     |
#> |&nbsp;&nbsp; maximum      |98.00                   |
#> |**KL**                    |&nbsp;&nbsp;            |
#> |&nbsp;&nbsp; FOUR         |4 (17.39%)              |
#> |&nbsp;&nbsp; ONE          |5 (21.74%)              |
#> |&nbsp;&nbsp; THREE        |5 (21.74%)              |
#> |&nbsp;&nbsp; TWO          |9 (39.13%)              |
#> |**WEDGE**                 |&nbsp;&nbsp;            |
#> |&nbsp;&nbsp; CWDFO        |3 (13.04%)              |
#> |&nbsp;&nbsp; CWHTO        |2 (8.70%)               |
#> |&nbsp;&nbsp; LCWDFO       |1 (4.35%)               |
#> |&nbsp;&nbsp; LCWHTO       |4 (17.39%)              |
#> |&nbsp;&nbsp; LOWDFO       |2 (8.70%)               |
#> |&nbsp;&nbsp; MCWDFO       |2 (8.70%)               |
#> |&nbsp;&nbsp; MOWDFO       |1 (4.35%)               |
#> |&nbsp;&nbsp; MOWHTO       |2 (8.70%)               |
#> |&nbsp;&nbsp; OWDFO        |3 (13.04%)              |
#> |&nbsp;&nbsp; OWHTO        |3 (13.04%)              |

Created on 2024-05-28 with reprex v2.1.0

Upvotes: 0

Related Questions