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?