Home > Software design >  Changing N in tbl_svysummary to be per 1000 or per 10000
Changing N in tbl_svysummary to be per 1000 or per 10000

Time:12-11

I am using the tbl_svysummary() function from the gtsummary package to create a table for a paper. My N, which I want to show in the header column, is in the millions. I'm trying to figure out how to convert it to be per 1000 or per 10000. For example:

data(api, package = "survey")
tbl_svysummary_ex2 <-
  survey::svydesign(id = ~dnum, weights = ~pw, data = apiclus1, fpc = ~fpc) %>%
  tbl_svysummary(by = "both", include = c(api00, stype))

Instead of N = 1692 for the No column in this example, I'd like it to be N = 1.692. For larger numbers, let's say N = 12,209,256, I'd want to divide by 10000 and display N = 1220 (per 10,000) or something like that.

I thought I could use modify_header() or modify the .$table_styling$header data.frame directly to do so, but that didn't seem to work.

CodePudding user response:

You can indeed use modify_header(). Example below!

library(gtsummary)
packageVersion("gtsummary")
#> [1] '1.6.3'

data(api, package = "survey")
survey::svydesign(id = ~dnum, weights = ~pw, data = apiclus1, fpc = ~fpc) %>%
  tbl_svysummary(by = "both", include = c(api00, stype)) %>%
  modify_header(all_stat_cols() ~ "**{level}** N={style_number(n, scale = 0.001, digits = 3)}K") %>%
  as_kable()
Characteristic No N=1.692K Yes N=4.502K
api00 631 (556, 710) 654 (551, 722)
stype
E 1,083 (64%) 3,791 (84%)
H 237 (14%) 237 (5.3%)
M 372 (22%) 474 (11%)

Created on 2022-12-09 with reprex v2.0.2

  • Related