Trying to create subgroups in Table

Trying to figure out how to structure my table into a major group with subheadings and it count both. For example,

Lymphoma Group n = 100
B Cell Lymphoma = 20
Hodgkins Lymphoma = 10
Skin Cancer = 50
Melanoma = 10
Merkel Cell = 5

I need to do this with several different groups but I'm unsure if there as a good way to do this or if i have to create a column for each subgroup? but will that still output the total number for the subgroup as well as the higher level grouping ?

structure(list(gender = c("Male", "Female", "Male", "Male", "Female", 
"Female", "Male", "Female", "Male", "Male", "Male", "Female", 
"Male", "Male", "Male", "Male", "Male", "Male", "Female", "Male", 
"Male", "Male", "Male", "Male", "Male", "Male", "Female", "Male", 
"Male", "Female", "Male", "Male", "Male", "Male", "Male", "Male", 
"Male", "Male", "Male", "Male", "Female", "Male", "Male", "Male", 
"Male", "Male", "Male", "Female", "Male", "Male", "Female", "Male", 
"Male", "Male", "Female", "Male", "Male", "Male", "Male", "Male", 
"Male", "Male", "Male", "Male", "Male", "Female", "Female", "Female", 
"Male", "Male", "Male", "Male", "Female", "Female", "Female", 
"Male", "Male", "Male", "Male", "Male", "Male", "Male", "Male", 
"Male", "Male", "Male", "Female", "Male", "Male", "Male", "Male", 
"Male", "Male", "Male", "Male", "Male", "Male", "Male", "Male", 
"Male"), Race_Ethnicity = c("Non-Hispanic White", "Non-Hispanic White", 
"Non-Hispanic White", "Hispanic", "Non-Hispanic White", "Non Hispanic Black", 
"Non-Hispanic White", "Non-Hispanic White", "Non-Hispanic White", 
"Non-Hispanic White", "Non-Hispanic White", "Non Hispanic Black", 
"Non Hispanic Black", "Non-Hispanic White", "Non Hispanic Black", 
"Non-Hispanic White", "Non-Hispanic White", "Non-Hispanic White", 
"Non Hispanic Black", "Non-Hispanic White", "Non-Hispanic White", 
"Non-Hispanic White", "Hispanic", "Non-Hispanic White", "Non-Hispanic White", 
"Non Hispanic Black", "Non Hispanic Black", "Non-Hispanic White", 
"Non-Hispanic White", "Non Hispanic Black", "Non-Hispanic White", 
"Non-Hispanic White", "Non Hispanic Black", "Non-Hispanic White", 
"Non-Hispanic White", "Non-Hispanic White", "Non-Hispanic White", 
"Hispanic", "Non Hispanic Black", "Non-Hispanic White", "Non Hispanic Black", 
"Non-Hispanic White", "Non-Hispanic White", "Non-Hispanic White", 
"Non-Hispanic White", "Non-Hispanic White", "Non-Hispanic White", 
"Non Hispanic Black", "Non-Hispanic White", "Non-Hispanic White", 
"Hispanic", "Non-Hispanic White", "Hispanic", "Non-Hispanic White", 
"Non Hispanic Black", "Non-Hispanic White", "Non-Hispanic White", 
"Non-Hispanic White", "Non Hispanic Black", "Non-Hispanic White", 
"Non-Hispanic White", "Non-Hispanic White", "Non-Hispanic White", 
"Non-Hispanic White", "Non-Hispanic White", "Non Hispanic Black", 
"Hispanic", "Hispanic", "Non-Hispanic White", "Non Hispanic Black", 
"Non-Hispanic White", "Non-Hispanic White", "Non Hispanic Black", 
"Hispanic", "Non-Hispanic White", "Non-Hispanic White", "Hispanic", 
"Hispanic", "Non-Hispanic White", "Non Hispanic Black", "Non-Hispanic White", 
"Non-Hispanic White", "Non-Hispanic White", "Non-Hispanic White", 
"Non-Hispanic White", "Non-Hispanic White", "Hispanic", "Non Hispanic Black", 
"Other", "Non-Hispanic White", "Non-Hispanic White", "Non Hispanic Black", 
"Non-Hispanic White", "Hispanic", "Non-Hispanic White", "Hispanic", 
"Non-Hispanic White", "Non Hispanic Black", "Non-Hispanic White", 
"Non-Hispanic White"), ThreeAgeGroup = c("Less than 50", "50-60", 
"Greater than 60", "50-60", "Less than 50", "Greater than 60", 
"Greater than 60", "50-60", "50-60", "Greater than 60", "50-60", 
"Less than 50", "Less than 50", "50-60", "Greater than 60", "50-60", 
"Less than 50", "Less than 50", "Less than 50", "Less than 50", 
"Less than 50", "Less than 50", "50-60", "50-60", "Less than 50", 
"50-60", "Less than 50", "Less than 50", "Less than 50", "50-60", 
"Less than 50", "Less than 50", "Less than 50", "Greater than 60", 
"Less than 50", "50-60", "Less than 50", "Less than 50", "Less than 50", 
"Less than 50", "50-60", "Less than 50", "Less than 50", "Less than 50", 
"Less than 50", "Less than 50", "Less than 50", "Less than 50", 
"Less than 50", "Less than 50", "Less than 50", "50-60", "Less than 50", 
"50-60", "Less than 50", "Less than 50", "Less than 50", "Less than 50", 
"Less than 50", "50-60", "Less than 50", "Less than 50", "Less than 50", 
"50-60", "Less than 50", "Less than 50", "50-60", "50-60", "Less than 50", 
"Greater than 60", "50-60", "50-60", "Greater than 60", "Greater than 60", 
"50-60", "Greater than 60", "Greater than 60", "50-60", "Greater than 60", 
"Less than 50", "Less than 50", "Greater than 60", "Less than 50", 
"50-60", "Less than 50", "50-60", "Greater than 60", "Less than 50", 
"Less than 50", "50-60", "50-60", "Less than 50", "Less than 50", 
"Less than 50", "Less than 50", "Less than 50", "Less than 50", 
"Greater than 60", "Greater than 60", "Less than 50"), vital_status = c("Dead", 
"Dead", "Alive", "Dead", "Dead", "Dead", "Alive", "Dead", "Alive", 
"Dead", "Dead", "Alive", "Dead", "Alive", "Alive", "Dead", "Alive", 
"Dead", "Dead", "Dead", "Dead", "Dead", "Dead", "Dead", "Alive", 
"Dead", "Dead", "Dead", "Dead", "Alive", "Dead", "Dead", "Dead", 
"Dead", "Dead", "Dead", "Alive", "Dead", "Alive", "Alive", "Dead", 
"Alive", "Dead", "Dead", "Dead", "Alive", "Dead", "Dead", "Dead", 
"Dead", "Alive", "Dead", "Dead", "Dead", "Dead", "Dead", "Alive", 
"Alive", "Dead", "Dead", "Alive", "Alive", "Dead", "Dead", "Dead", 
"Alive", "Alive", "Dead", "Alive", "Dead", "Dead", "Alive", "Alive", 
"Dead", "Dead", "Dead", "Dead", "Dead", "Alive", "Dead", "Dead", 
"Dead", "Dead", "Alive", "Alive", "Alive", "Alive", "Alive", 
"Alive", "Dead", "Alive", "Dead", "Dead", "Dead", "Dead", "Dead", 
"Dead", "Dead", "Dead", "Dead"), survivalmonths = c(6.70170827858081, 
52.6938239159001, 75.9855453350854, 7.45729303547963, 96.484888304862, 
6.11038107752957, 52.1681997371879, 8.86990801576873, 124.737187910644, 
19.9408672798949, 13.9618922470434, 20.26938239159, 185.643889618922, 
107.358738501971, 212.680683311432, 7.16162943495401, 203.876478318003, 
5.28909329829172, 32.8186596583443, 143.23258869908, 5.81471747700394, 
9.85545335085414, 1.21550591327201, 90.1116951379763, 197.043363994744, 
72.634691195795, 5.09198423127464, 9.19842312746386, 24.5400788436268, 
174.540078843627, 4.92772667542707, 168.92247043364, 10.9395532194481, 
6.50459921156373, 15.407358738502, 17.1484888304862, 151.34691195795, 
26.5111695137976, 183.77135348226, 183.0814717477, 11.7279894875164, 
183.344283837057, 12.3193166885677, 4.40210249671485, 136.235216819974, 
196.189224704336, 23.1931668856767, 18.0354796320631, 3.87647831800263, 
43.2982917214192, 195.499342969777, 12.4507227332457, 70.4007884362681, 
33.8370565045992, 195.335085413929, 25.3285151116951, 211.070959264126, 
173.817345597897, 58.4756898817346, 22.9960578186597, 173.390275952694, 
177.529566360053, 21.550591327201, 177.792378449409, 8.3442838370565, 
73.5216819973719, 172.798948751643, 21.8791064388962, 167.706964520368, 
21.5834428383706, 29.1064388961892, 65.6373193166886, 62.8449408672799, 
5.22339027595269, 122.010512483574, 71.6491458607096, 10.7752956636005, 
4.07358738501971, 206.931668856767, 7.26018396846255, 161.695137976347, 
9.7568988173456, 13.2063074901445, 165.867279894875, 160.84099868594, 
161.202365308804, 9.82260183968462, 191.754270696452, 159.986859395532, 
25.065703022339, 174.047306176084, 146.123521681997, 26.3797634691196, 
2.10249671484888, 94.053876478318, 3.8107752956636, 114.618922470434, 
184.165571616294, 32.227332457293, 13.2720105124836), SEERDContactStage = c("Localized", 
"Localized", "Localized", "Distant", "Regional", "Distant", "Localized", 
"Regional", "Regional", "Localized", "Localized", "Localized", 
"Distant", "Distant", "Localized", "Localized", "Localized", 
"Distant", "Distant", "Localized", "Not Specified", "Distant", 
"Distant", "Localized", "Localized", "Regional", "Distant", "Distant", 
"Localized", "Regional", "Localized", "Localized", "Distant", 
"Regional", "Distant", "Localized", "Localized", "Regional", 
"Distant", "Localized", "Regional", "Localized", "Distant", "Distant", 
"Localized", "Localized", "Distant", "Regional", "Regional", 
"Distant", "Not Specified", "Distant", "Distant", "Localized", 
"Regional", "Localized", "Localized", "Distant", "Localized", 
"Distant", "Localized", "Regional", "Regional", "Localized", 
"Distant", "Regional", "Localized", "Regional", "Regional", "Distant", 
"Regional", "Regional", "Localized", "Regional", "Localized", 
"Localized", "Localized", "Localized", "Localized", "Distant", 
"Localized", "Distant", "Distant", "Localized", "Not Specified", 
"Distant", "Localized", "Distant", "Distant", "Distant", "Localized", 
"Distant", "Distant", "Localized", "Distant", "Distant", "Localized", 
"Not Specified", "Localized", "Distant"), HIVDXYGroup = c("1997-2008", 
NA, "1996 or Earlier", "1997-2008", "1997-2008", "1996 or Earlier", 
"2009 or Later", "1997-2008", "1997-2008", NA, "1996 or Earlier", 
NA, "1997-2008", "1996 or Earlier", "1996 or Earlier", "2009 or Later", 
NA, "1996 or Earlier", NA, "1996 or Earlier", "1996 or Earlier", 
"1997-2008", "1997-2008", NA, "1997-2008", "1997-2008", "1996 or Earlier", 
"1996 or Earlier", "1996 or Earlier", "1996 or Earlier", NA, 
NA, "2009 or Later", "1996 or Earlier", "1997-2008", "1996 or Earlier", 
NA, "1996 or Earlier", "1997-2008", "1996 or Earlier", "1997-2008", 
"1996 or Earlier", "1997-2008", "1997-2008", "1996 or Earlier", 
"1997-2008", "1997-2008", NA, "1996 or Earlier", "1997-2008", 
"1996 or Earlier", NA, "1997-2008", NA, "1997-2008", "1996 or Earlier", 
"1997-2008", "1997-2008", "1997-2008", "1997-2008", "1997-2008", 
"1997-2008", NA, "1997-2008", "1997-2008", NA, NA, NA, "1997-2008", 
"1997-2008", NA, "1997-2008", "1996 or Earlier", NA, "1997-2008", 
"1996 or Earlier", NA, NA, "1996 or Earlier", "1997-2008", "1997-2008", 
"1997-2008", "1997-2008", "1997-2008", "1996 or Earlier", "1997-2008", 
NA, "1997-2008", "1997-2008", "1997-2008", "1996 or Earlier", 
"1996 or Earlier", "1996 or Earlier", NA, "1997-2008", "1997-2008", 
"1996 or Earlier", NA, "1996 or Earlier", "1997-2008"), HIVDuration = c("0-2 years", 
NA, "Over 20 years", "6-10 years", "11-20 years", "11-20 years", 
"3-5 years", "6-10 years", "Over 20 years", NA, "Over 20 years", 
NA, "11-20 years", "Over 20 years", "Over 20 years", "0-2 years", 
NA, "11-20 years", NA, "Over 20 years", "11-20 years", "6-10 years", 
"0-2 years", NA, "Over 20 years", "11-20 years", "11-20 years", 
"6-10 years", "Over 20 years", "Over 20 years", NA, NA, "6-10 years", 
"11-20 years", "3-5 years", "Over 20 years", NA, "Over 20 years", 
"11-20 years", "Over 20 years", "0-2 years", "Over 20 years", 
"3-5 years", "3-5 years", "Over 20 years", "11-20 years", "6-10 years", 
NA, "11-20 years", "6-10 years", "Over 20 years", NA, "11-20 years", 
NA, "11-20 years", "11-20 years", "11-20 years", "11-20 years", 
"6-10 years", "3-5 years", "11-20 years", "11-20 years", NA, 
"11-20 years", "0-2 years", NA, NA, NA, "11-20 years", "3-5 years", 
NA, "11-20 years", "Over 20 years", NA, "11-20 years", "Over 20 years", 
NA, NA, "Over 20 years", "6-10 years", "11-20 years", "6-10 years", 
"6-10 years", "11-20 years", "Over 20 years", "11-20 years", 
NA, "Over 20 years", "11-20 years", "6-10 years", "Over 20 years", 
"Over 20 years", "Over 20 years", NA, "6-10 years", "3-5 years", 
"Over 20 years", NA, "11-20 years", "3-5 years"), CancerDXYGroup = c("2004-2009", 
"2016 or Later", "2016 or Later", "2010-2015", "2004-2009", "2004-2009", 
"2016 or Later", "2004-2009", "2010-2015", "2010-2015", "2004-2009", 
"2016 or Later", "2004-2009", "2010-2015", "2004-2009", "2010-2015", 
"2004-2009", "2004-2009", "2004-2009", "2004-2009", "2004-2009", 
"2004-2009", "2004-2009", "2004-2009", "2004-2009", "2004-2009", 
"2004-2009", "2004-2009", "2004-2009", "2004-2009", "2004-2009", 
"2004-2009", "2016 or Later", "2004-2009", "2004-2009", "2004-2009", 
"2004-2009", "2004-2009", "2004-2009", "2004-2009", "2004-2009", 
"2004-2009", "2004-2009", "2004-2009", "2004-2009", "2004-2009", 
"2004-2009", "2004-2009", "2004-2009", "2004-2009", "2004-2009", 
"2004-2009", "2004-2009", "2010-2015", "2004-2009", "2004-2009", 
"2004-2009", "2004-2009", "2004-2009", "2004-2009", "2004-2009", 
"2004-2009", "2004-2009", "2004-2009", "2004-2009", "2016 or Later", 
"2004-2009", "2004-2009", "2004-2009", "2004-2009", "2004-2009", 
"2010-2015", "2016 or Later", "2016 or Later", "2004-2009", "2010-2015", 
"2016 or Later", "2016 or Later", "2004-2009", "2004-2009", "2004-2009", 
"2004-2009", "2004-2009", "2004-2009", "2004-2009", "2004-2009", 
"2016 or Later", "2004-2009", "2004-2009", "2004-2009", "2004-2009", 
"2004-2009", "2004-2009", "2004-2009", "2004-2009", "2004-2009", 
"2004-2009", "2004-2009", "2004-2009", "2004-2009"), HigherCancerGroup = c("Sarcoma", 
"Other Rare Cancers", "HPV-Related Cancer", "Lung Cancer", "Breast Cancer", 
"Gastrointestinal Cancers", "Lymphoma", "Head and Neck", "HPV-Related Cancer", 
"Genitourinary Cancers", "Sarcoma", "HPV-Related Cancer", "Lymphoma", 
"Lymphoma", "Gastrointestinal Cancers", "Lymphoma", "Other Rare Cancers", 
"Leukemia/Myeloproliferative", "Head and Neck", "Sarcoma", "Lymphoma", 
"Lymphoma", "Lymphoma", "Skin Cancer", "Lung Cancer", "Lung Cancer", 
"Gastrointestinal Cancers", "HPV-Related Cancer", "Skin Cancer", 
"Lymphoma", "Head and Neck", "Genitourinary Cancers", "Leukemia/Myeloproliferative", 
"Gastrointestinal Cancers", "Leukemia/Myeloproliferative", "Gastrointestinal Cancers", 
"Genitourinary Cancers", "Lymphoma", "Lymphoma", "Skin Cancer", 
"Gastrointestinal Cancers", "Gastrointestinal Cancers", "Lymphoma", 
"Lymphoma", "Gastrointestinal Cancers", "Lymphoma", "Head and Neck", 
"HPV-Related Cancer", "Lymphoma", "Lung Cancer", "Lymphoma", 
"Gastrointestinal Cancers", "Lymphoma", "Head and Neck", "Lymphoma", 
"Gastrointestinal Cancers", "Genitourinary Cancers", "Lymphoma", 
"Genitourinary Cancers", "Lymphoma", "Sarcoma", "Genitourinary Cancers", 
"Head and Neck", "HPV-Related Cancer", "Leukemia/Myeloproliferative", 
"HPV-Related Cancer", "Genitourinary Cancers", "Gastrointestinal Cancers", 
"Lymphoma", "Genitourinary Cancers", "Gastrointestinal Cancers", 
"Gastrointestinal Cancers", "Lung Cancer", "Genitourinary Cancers", 
"Sarcoma", "HPV-Related Cancer", "Lymphoma", "Sarcoma", "Genitourinary Cancers", 
"Leukemia/Myeloproliferative", "Lymphoma", "Gastrointestinal Cancers", 
"Sarcoma", "Genitourinary Cancers", "Leukemia/Myeloproliferative", 
"Lymphoma", "Lymphoma", "Lymphoma", "Lymphoma", "Leukemia/Myeloproliferative", 
"HPV-Related Cancer", "Lymphoma", "Lymphoma", "Lymphoma", "Lymphoma", 
"Gastrointestinal Cancers", "HPV-Related Cancer", "Genitourinary Cancers", 
"Skin Cancer", "Lymphoma")), row.names = c(NA, -100L), class = c("tbl_df", 
"tbl", "data.frame")) 

DemographicsTab1 <- DemographicData %>% select(gender, Race_Ethnicity, ThreeAgeGroup, vital_status, survivalmonths,
                    SEERDContactStage, HIVDXYGroup, HIVDuration, CancerDXYGroup, HigherCancerGroup) %>%
  tbl_summary(
    missing = "ifany",
    label = list(
      gender ~ "Gender",
      Race_Ethnicity ~ "Demographic",
      vital_status ~ "Vital Status",
      SEERDContactStage ~ "SEER Stage at First Contact",
      ThreeAgeGroup ~ "Age Groups",
      survivalmonths ~ "Survival Time (months)",
      HIVDXYGroup ~ "HIV DX Years",
      HIVDuration ~ "HIV Infection Duration",
      CancerDXYGroup ~ "Cancer Diagnosis Years",
      HigherCancerGroup ~ "Cancer Type"
      )) %>% modify_caption("**Table 1. Patient Characteristics**") %>%
  bold_labels()

Also, Is there a way to sort the variables specifically? For example, age group by youngest to oldest and "SEER Stage" in a specific order for example, local then regional then distant and then not specified/unknown

Welcome to the forum.

I think we, probably, need to see some sample data.

Do

dput(mydata)  

If you have a really large data set, probably,




dput(head(mydata, 100)) 

will do where "mydata" is the name of your data file.
Copy the output and paste it between

```

```

Also can you give us he code you are using?
Copy the code and paste it between

```

``

Thank you for the help!

I only included a portion of the data since I have so much and the main focus was trying to figure out how to group the Cancer SubTypes under their respective HigherCancerGroup. Let me know if you want me to include the other variables as well.

Thanks for the data but it is not a complete dput output and I don't know what to make of it.

You appear to be missing one or more lines at the top.
See my very simple example below.

mydat <- data.frame(aa = LETTERS[1:10],  bb = 1:10, cc = 10:1)
dput(mydat)

Output

structure(list(aa = c("A", "B", "C", "D", "E", "F", "G", "H", 
"I", "J"), bb = 1:10, cc = 10:1), class = "data.frame", row.names = c(NA, 
-10L))

We are missing the structure(list( and the name of the first variable and probably some data in your post.

Given your code it looks like you are using a lot of variables so you probably should include them all. It usually does no harm.

BTW, for the ``` to work it must be on a separate line like this
```
2 =2
```

thank you again, sorry about any misunderstandings. First time using R and doing something like this.

Not a problem. I try not to remember the number of things I did incorrectly when first using R. I also try to forget the really dumb things I have done recently. Don't ask!

That new data looks good. It's getting late here (18:47 EST) so I will have to leave this until tomorrow. I'm getting calls for dinner.