1

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.

  • by after the comma, you mean the decimal place right? I just ask because where I'm from, we don't use commas, we use periods for that (e.g. a half is 0.5) – Mark Aug 03 '23 at 12:14
  • also, your code fails for me with `✖ Problematic argument: • group = cat1` – Mark Aug 03 '23 at 12:16
  • Thanks for the response! Yes, I mean the decimal place. As for the code, it returns the same warning but still runs the code. Hopefully, it does the same on your end? – blue-create Aug 03 '23 at 12:19
  • it does! I wasn't sure if that was the intention though phew – Mark Aug 03 '23 at 12:22
  • re: decimal place - it's all good! It varies from place to place – Mark Aug 03 '23 at 12:24
  • re: it being 100, not 99 or 101 - So how would you want to do that? Because (for the sake of argument) if you had three categories, one with 49.5%, another with 30.5%, and another with 20%, the two with .5 could get rounded up or down – Mark Aug 03 '23 at 12:27
  • 1
    I like this approach here but don't know how to implement it for gtsummary: https://stackoverflow.com/questions/40515869/rounding-percentages-to-100-in-r – blue-create Aug 03 '23 at 12:29
  • it looks like `tbl_svysummary` doesn't allow you to use custom functions, but `tbl_custom_summary` does – Mark Aug 03 '23 at 12:35

0 Answers0