There's some solutions on rounding percentages to 100 % in general for vectors and across tables but I haven't found an implementation inside the gtsummary tbl_svysummary()
or tbl_summary()
function yet.
I have a weighted dataframe (see example below) with categorical and numerical variables. I am interested in getting the proportions and mean values. However, for the categorical variables, it'd be great if the individual proportions could amount to 100, instead of 99 and 101.
I was wondering whether there are any ways to make the proportions of responses in categorical variables, i.e., the percentages, round up to perfectly 100 % with 0 or 2 digits after the decimal place?
Thanks in advance!
Here's some example code:
library(dplyr)
library(gtsummary)
set.seed(123)
numerical_var <- rnorm(200, mean = 50, sd = 10)
categories <- c("Category_A", "Category_B", "Category_C", "Category_D")
categorical_var1 <- sample(categories, 200, replace = TRUE)
categorical_var2 <- sample(categories, 200, replace = TRUE)
categorical_var3 <- sample(categories, 200, replace = TRUE)
weight_var <- runif(200, min = 0.003, max = 40)
# Create the data frame
df<- data.frame(
num = numerical_var,
cat1 = categorical_var1,
cat2 = categorical_var2,
cat3 = categorical_var3,
wgt = weight_var
)
# Create design object
svy_design <- survey::svydesign(
~1,
data = df,
weights = ~wgt
)
# Get frequencies
svy_table <- svy_design %>%
tbl_svysummary(
by = cat1,
statistic = all_categorical() ~ "{p}%"
) %>%
add_p(
group = cat1
)
svy_table %>%
gtsummary::as_tibble() %>%
writexl::write_xlsx(
., "test.xlsx"
)
)
Edit: added a bit more context info and changed comma to decimal place.