how to create tables with gt library in R?

I want to create tables with gt library, similar to the one in the picture bellow

This is the type of data I have:

structure(list(id = c("2ea06a3a96", "76a7e5d251", "29a0c9f7d9", 
"4d1188caef", "970c38434a", "96dbb11b61", "acff8511c6", "a1ddf9ecba", 
"425d60b46a", "ec601cd8c5", "42cc8e980c", "822b9c9da6", "edb56bac99", 
"095e5079df", "95c0096cb3", "677e9b44e7", "3b53e377eb", "114f886911", 
"2fa12d6165", "f2c6311363", "0677c4cfc4", "a766a6652a", "29d5739a95", 
"6a17fc2e03", "ae5a737a81", "e2da9843fe", "f038e96abb", "96765b4286", 
"7be540aca3", "26d5b180bf", "c5262c0f01", "7b8f39561d", "5af251e51d", 
"ed6b830e6d", "e3be3d357c", "13fdd1b749", "72f4bdf464", "bbdea149be", 
"83161a15ad", "cbe89428dd", "6c337c3a95", "ad41833bd7", "e0b51db8fd", 
"b58b8e9e12", "8c0e68b993", "12c73b1976", "382f2e5eb9", "af00d8a6ec", 
"1fd4e5714b", "287ac54048"), tested = c("negative", "showing symptoms", 
"negative", "negative", "showing symptoms", "negative", "showing symptoms", 
"negative", "negative", "showing symptoms", "negative", "negative", 
"negative", "negative", "negative", "showing symptoms", "negative", 
"showing symptoms", "showing symptoms", "negative", "showing symptoms", 
"showing symptoms", "negative", "negative", "negative", "negative", 
"negative", "negative", "negative", "showing symptoms", "showing symptoms", 
"negative", "negative", "negative", "negative", "negative", "negative", 
"showing symptoms", "negative", "negative", "showing symptoms", 
"negative", "negative", "negative", "negative", "negative", "negative", 
"negative", "negative", "showing symptoms"), age_band = c("0-19", 
"20-39", "20-39", "40-59", "20-39", "60+", "40-59", "20-39", 
"40-59", "20-39", "20-39", "20-39", "40-59", "40-59", "40-59", 
"20-39", "40-59", "20-39", "20-39", "40-59", "40-59", "20-39", 
"20-39", "20-39", "20-39", "40-59", "40-59", "40-59", "20-39", 
"20-39", "40-59", "20-39", "20-39", "20-39", "20-39", "20-39", 
"40-59", "20-39", "40-59", "20-39", "40-59", "20-39", "40-59", 
"20-39", "20-39", "60+", "20-39", "20-39", "20-39", "20-39"), 
    asthma = c("No", "No", "No", "No", "No", "No", "Yes", "No", 
    "No", "No", "No", "No", "No", "No", "No", "No", "Yes", "Yes", 
    "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", 
    "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", 
    "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", 
    "No", "No"), diabetes_type_one = c("No", "No", "No", "No", 
    "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", 
    "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", 
    "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", 
    "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", 
    "No", "No", "No", "No", "No", "No"), diabetes_type_two = c("No", 
    "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", 
    "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", 
    "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", 
    "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", 
    "No", "No", "No", "No", "No", "No", "No", "Yes", "No"), obesity = c("No", 
    "Yes", "No", "No", "No", "No", "No", "No", "No", "No", "Yes", 
    "No", "No", "No", "No", "No", "No", "No", "No", "Yes", "Yes", 
    "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", 
    "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", 
    "No", "No", "No", "No", "No", "No", "No", "Yes", "No"), hypertension = c("No", 
    "Yes", "No", "No", "No", "No", "Yes", "No", "No", "No", "No", 
    "No", "No", "No", "No", "No", "No", "No", "No", "No", "Yes", 
    "No", "No", "No", "No", "No", "No", "No", "No", "No", "Yes", 
    "No", "No", "No", "No", "No", "No", "Yes", "No", "No", "Yes", 
    "No", "Yes", "No", "No", "No", "No", "No", "Yes", "No"), 
    heart_disease = c("No", "No", "No", "No", "No", "No", "No", 
    "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", 
    "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", 
    "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", 
    "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", 
    "No", "No", "No"), lung_condition = c("No", "No", "No", "No", 
    "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", 
    "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", 
    "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", 
    "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", 
    "No", "No", "No", "No", "No", "No"), liver_disease = c("No", 
    "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", 
    "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", 
    "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", 
    "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", 
    "No", "No", "No", "No", "No", "No", "No", "No", "No"), kidney_disease = c("No", 
    "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", 
    "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", 
    "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", 
    "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", 
    "No", "No", "No", "No", "No", "No", "No", "No", "No")), row.names = c(NA, 
-50L), class = c("tbl_df", "tbl", "data.frame"))

This is what I have done:

numb <- test  %>%
  tidyr::pivot_longer(cols= 4:12, names_to="comorbidities", values_to="yes_no") %>%
  dplyr::group_by(age_band, tested, comorbidities, yes_no) %>%
  dplyr::summarise(count=n()) %>%
  dplyr::mutate(perc =  count/sum(count) *100) %>%
  dplyr::filter(yes_no !="No" & age_band != "0-19" & tested != "negative")

numb$yes_no <- NULL


numb$comorbidities <- as.factor(numb$comorbidities)
numb$age_band <- as.factor(numb$age_band)
numb$perc <- round(numb$perc, digits = 1)

numb

With my specific data - I want Age Band 20-39; 40-59 and 60+ just like 2015; 2016; 2017 in the table above and right under each age group I want tested and showing symptoms categories just like write and pass rate for each comorbidities in alphabetical order - asthma, cough, chills etc (which should appear on the side as rows - just like 'Ontario RN university program collaborative partner, and language instruction'. I want counts and percentages to be kept.

I just found out about this library but do not know how to get the 3 different groups of age band in a column(which appear into rows) and the the two groups positive and showing symptoms, the same stored into rows.

There's lots of good documentation for gt. For grouping and summary rows this vignette should help:


And the intro to the package should cover the rest. If you get stuck, please post a reprex (short for reproducible example) here

The parts of the table you're referring to are all in the helpful diagram in the docs.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.