-1

I need to draw 3 way HTML tables with frequency and row percentages. I like gtsummary to draw 2 way HTML tables but do not know how to draw 3 way. So, I tried xtab but can't figure out how to add frequency and row-percentage in the same table. Please suggest me how to draw a nice HTML table with frequency and row percentages. Here is reproducible example.

library(vcd)
head(Arthritis)   # top few observations from the data
##   ID Treatment  Sex Age Improved
## 1 57   Treated Male  27     Some
## 2 46   Treated Male  29     None
## 3 77   Treated Male  30     None
## 4 17   Treated Male  32   Marked
## 5 36   Treated Male  46   Marked
## 6 23   Treated Male  58   Marked
mytable <- xtabs(~ Treatment+Sex+Improved, data=Arthritis)
mytable
## , , Improved = None
## 
##          Sex
## Treatment Female Male
##   Placebo     19   10
##   Treated      6    7
## 
## , , Improved = Some
## 
##          Sex
## Treatment Female Male
##   Placebo      7    0
##   Treated      5    2
## 
## , , Improved = Marked
## 
##          Sex
## Treatment Female Male
##   Placebo      6    1
##   Treated     16    5

I need to add row percentage and also need to make it HTML. If I can do it with gtsummary, that's the best. But I am willing to use any other packages such as kableExtra.

HSC
  • 129
  • 6
  • 2
    Please consider making your question reproducible by generating an example dataset and matching expected output. See [How to make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for more information. – Ian Campbell May 27 '21 at 19:57

1 Answers1

1

Perhaps something like this will work for you:

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

tbl <-
  trial %>%
  tbl_strata(
    strata = trt, 
    ~.x %>%
      tbl_cross(stage, grade, percent = "row", margin = NULL),
    .combine_with = "tbl_stack"
  )

enter image description here Created on 2021-05-27 by the reprex package (v2.0.0)

Daniel D. Sjoberg
  • 8,820
  • 2
  • 12
  • 28