Skip to contents

Getting Started

First, set up your working environment by loading these 4 packages using the library() function. Note that you can copy any of the code blocks below by hovering over the top right corner, clicking on the copy icon that appears, and them paste these into your local version of R to run them.

Counting Data in Tables

It is common to count data, particularly in categories, to summarize characteristics or outcomes. The tabyl function in the {janitor} package is helpful for this.

In this vignette, we will look at how to use this function to make simple tables of counts of your data.

First, we will read in the data from strep_tb and indo_rct to use for our tables. Load the libraries in the setup chunk above. If you then run the code chunk below, you should have two new data objects in your Environment tab.

strep_tb <- medicaldata::strep_tb
indo_rct <- medicaldata::indo_rct

Table 2 from Streptomycin for Tuberculosis

We will now try to reproduce Table 2 from the Streptomycin for Tuberculosis manuscript, which can be found here on page 771. These are the primary endpoint results, summarized in a 6 rows x 2 columns table (with an added totals row). Let’s walk through how to do this with the tabyl() function in the {janitor} package.

First Table

The tabyl() function allows you to pipe data into tables, and add ‘adornments’ like total rows and percentages. Let’s start with a basic one-variable tabyl using this ordinal endpoint. You can pipe the dataset into the tabyl function, with your desired variable (radiologic_6m) as the only argument to the function.

strep_tb %>% 
  tabyl(radiologic_6m)
#>                 radiologic_6m  n    percent
#>                       1_Death 18 0.16822430
#>  2_Considerable_deterioration 12 0.11214953
#>      3_Moderate_deterioration 17 0.15887850
#>                   4_No_change  5 0.04672897
#>        5_Moderate_improvement 23 0.21495327
#>    6_Considerable_improvement 32 0.29906542

This gives us the n and proportion of each level of the primary outcome.

Two Variable Table

If we add the treatment arm variable as a 2nd argument, we can come closer to the original table. Note that the levels of the first argument make up the rows of the table, and that the levels of the 2nd argument make up the columns (standard R x C order). Also note that with 2 variables, we get the counts by default, but not proportions of each level, as you might want proportions that are column-wise, or row-wise.

strep_tb %>% 
  tabyl(radiologic_6m, arm)
#>                 radiologic_6m Control Streptomycin
#>                       1_Death      14            4
#>  2_Considerable_deterioration       6            6
#>      3_Moderate_deterioration      12            5
#>                   4_No_change       3            2
#>        5_Moderate_improvement      13           10
#>    6_Considerable_improvement       4           28

Add A Total Row

This is closer, but lacks the total row, and the percentages. In order to have numbers to calculate totals, we have to start with the total row first. We will ‘adorn’ the table with a totals row. We have to specify that we want an additional row of totals at the bottom (not a column of row-wise totals in a new column on the right), with the where argument to the adorn_totals function.

strep_tb %>% 
  tabyl(radiologic_6m, arm) %>% 
  adorn_totals(where = "row") # add a total row
#>                 radiologic_6m Control Streptomycin
#>                       1_Death      14            4
#>  2_Considerable_deterioration       6            6
#>      3_Moderate_deterioration      12            5
#>                   4_No_change       3            2
#>        5_Moderate_improvement      13           10
#>    6_Considerable_improvement       4           28
#>                         Total      52           55

Add Percentages and formatting

This is closer. Now we need to add the percentages, and percentage formatting. We need to specify that we want column-wise, rather than row-wise percentages. We then have to adorn_ns to add the counts, so that we have both counts and percentages. We can specify that we want the counts to be listed first, with the argument position = "front" in the adorn_ns function.

strep_tb %>% 
  tabyl(radiologic_6m, arm) %>% #2 dimensional table, RxC
  adorn_totals(where = "row") %>% # add totals row
  adorn_percentages("col") %>%  # column-wise percentages
  adorn_pct_formatting() %>% 
  adorn_ns(position = "front")  # put n first
#>                 radiologic_6m     Control Streptomycin
#>                       1_Death 14  (26.9%)   4   (7.3%)
#>  2_Considerable_deterioration  6  (11.5%)   6  (10.9%)
#>      3_Moderate_deterioration 12  (23.1%)   5   (9.1%)
#>                   4_No_change  3   (5.8%)   2   (3.6%)
#>        5_Moderate_improvement 13  (25.0%)  10  (18.2%)
#>    6_Considerable_improvement  4   (7.7%)  28  (50.9%)
#>                         Total 52 (100.0%)  55 (100.0%)

Making it Pretty

You can pipe this table into a flextable() object, which makes it easy to add fancy formatting. There are many formatting options in the flextable package, which you can learn about here. You can control column width, fonts, colors, and much more once you are in flextable format. Flextables can be output to MS Word, powerpoint, HTML, and PDF, through Rmarkdown.

strep_tb %>% 
  tabyl(radiologic_6m, arm) %>% 
  adorn_totals(where = "row") %>% 
  adorn_percentages("col") %>%  # column-wise percentages
  adorn_pct_formatting() %>% 
  adorn_ns(position = "front") %>%   # put n first
  flextable::flextable()

radiologic_6m

Control

Streptomycin

1_Death

14 (26.9%)

4 (7.3%)

2_Considerable_deterioration

6 (11.5%)

6 (10.9%)

3_Moderate_deterioration

12 (23.1%)

5 (9.1%)

4_No_change

3 (5.8%)

2 (3.6%)

5_Moderate_improvement

13 (25.0%)

10 (18.2%)

6_Considerable_improvement

4 (7.7%)

28 (50.9%)

Total

52 (100.0%)

55 (100.0%)

Try this Yourself

Now try this yourself, but instead of using the ordinal radiologic_6m outcome, use the improved dichotomous outcome in its place. Copy the code block below and add the piping and additional lines to produce a 2 x 2 table of outcomes. You can websearch for janitor tabyl adorn_title to learn how to add a title to your table.

strep_tb
#>     patient_id          arm dose_strep_g dose_PAS_g gender baseline_condition
#> 1            1      Control            0          0      M             1_Good
#> 2            2      Control            0          0      F             1_Good
#> 3            3      Control            0          0      F             1_Good
#> 4            4      Control            0          0      M             1_Good
#> 5            5      Control            0          0      F             1_Good
#> 6            6      Control            0          0      M             1_Good
#> 7            7      Control            0          0      F             1_Good
#> 8            8      Control            0          0      M             1_Good
#> 9            9      Control            0          0      F             2_Fair
#> 10          10      Control            0          0      M             2_Fair
#> 11          11      Control            0          0      F             2_Fair
#> 12          12      Control            0          0      M             2_Fair
#> 13          13      Control            0          0      F             2_Fair
#> 14          14      Control            0          0      M             2_Fair
#> 15          15      Control            0          0      F             2_Fair
#> 16          16      Control            0          0      M             2_Fair
#> 17          17      Control            0          0      F             2_Fair
#> 18          18      Control            0          0      M             2_Fair
#> 19          19      Control            0          0      F             2_Fair
#> 20          20      Control            0          0      M             2_Fair
#> 21          21      Control            0          0      F             2_Fair
#> 22          22      Control            0          0      M             2_Fair
#> 23          23      Control            0          0      F             2_Fair
#> 24          24      Control            0          0      M             2_Fair
#> 25          25      Control            0          0      F             2_Fair
#> 26          26      Control            0          0      M             2_Fair
#> 27          27      Control            0          0      F             2_Fair
#> 28          28      Control            0          0      M             2_Fair
#> 29          29      Control            0          0      F             3_Poor
#> 30          30      Control            0          0      F             3_Poor
#> 31          31      Control            0          0      M             3_Poor
#> 32          32      Control            0          0      F             3_Poor
#> 33          33      Control            0          0      M             3_Poor
#> 34          34      Control            0          0      F             3_Poor
#> 35          35      Control            0          0      M             3_Poor
#> 36          36      Control            0          0      F             3_Poor
#> 37          37      Control            0          0      M             3_Poor
#> 38          38      Control            0          0      F             3_Poor
#> 39          39      Control            0          0      F             3_Poor
#> 40          40      Control            0          0      M             3_Poor
#> 41          41      Control            0          0      F             3_Poor
#> 42          42      Control            0          0      M             3_Poor
#> 43          43      Control            0          0      F             3_Poor
#> 44          44      Control            0          0      M             3_Poor
#> 45          45      Control            0          0      F             3_Poor
#> 46          46      Control            0          0      F             3_Poor
#> 47          47      Control            0          0      F             3_Poor
#> 48          48      Control            0          0      M             3_Poor
#> 49          49      Control            0          0      F             3_Poor
#> 50          50      Control            0          0      M             3_Poor
#> 51          51      Control            0          0      F             3_Poor
#> 52          52      Control            0          0      M             3_Poor
#> 53          53 Streptomycin            2          0      M             1_Good
#> 54          55 Streptomycin            2          0      F             1_Good
#> 55          56 Streptomycin            2          0      M             1_Good
#> 56          57 Streptomycin            2          0      F             1_Good
#> 57          58 Streptomycin            2          0      M             1_Good
#> 58          59 Streptomycin            2          0      F             1_Good
#> 59          60 Streptomycin            2          0      M             1_Good
#> 60          67 Streptomycin            2          0      F             1_Good
#> 61          74 Streptomycin            2          0      M             2_Fair
#> 62          54 Streptomycin            2          0      F             2_Fair
#> 63          61 Streptomycin            2          0      F             2_Fair
#> 64          68 Streptomycin            2          0      M             2_Fair
#> 65          75 Streptomycin            2          0      F             2_Fair
#> 66          62 Streptomycin            2          0      M             2_Fair
#> 67          63 Streptomycin            2          0      F             2_Fair
#> 68          64 Streptomycin            2          0      M             2_Fair
#> 69          65 Streptomycin            2          0      F             2_Fair
#> 70          66 Streptomycin            2          0      M             2_Fair
#> 71          69 Streptomycin            2          0      F             2_Fair
#> 72          70 Streptomycin            2          0      M             2_Fair
#> 73          71 Streptomycin            2          0      F             2_Fair
#> 74          72 Streptomycin            2          0      M             2_Fair
#> 75          73 Streptomycin            2          0      F             2_Fair
#> 76          81 Streptomycin            2          0      F             2_Fair
#> 77          88 Streptomycin            2          0      F             2_Fair
#> 78          82 Streptomycin            2          0      F             3_Poor
#> 79          89 Streptomycin            2          0      M             3_Poor
#> 80          76 Streptomycin            2          0      M             3_Poor
#> 81          77 Streptomycin            2          0      F             3_Poor
#> 82          78 Streptomycin            2          0      M             3_Poor
#> 83          79 Streptomycin            2          0      F             3_Poor
#> 84          80 Streptomycin            2          0      M             3_Poor
#> 85          83 Streptomycin            2          0      M             3_Poor
#> 86          84 Streptomycin            2          0      F             3_Poor
#> 87          85 Streptomycin            2          0      M             3_Poor
#> 88          86 Streptomycin            2          0      F             3_Poor
#> 89          87 Streptomycin            2          0      M             3_Poor
#> 90          90 Streptomycin            2          0      F             3_Poor
#> 91          91 Streptomycin            2          0      F             3_Poor
#> 92          95 Streptomycin            2          0      F             3_Poor
#> 93         102 Streptomycin            2          0      M             3_Poor
#> 94          96 Streptomycin            2          0      M             3_Poor
#> 95         103 Streptomycin            2          0      F             3_Poor
#> 96          92 Streptomycin            2          0      M             3_Poor
#> 97          93 Streptomycin            2          0      F             3_Poor
#> 98          94 Streptomycin            2          0      M             3_Poor
#> 99          97 Streptomycin            2          0      F             3_Poor
#> 100         98 Streptomycin            2          0      F             3_Poor
#> 101         99 Streptomycin            2          0      F             3_Poor
#> 102        100 Streptomycin            2          0      M             3_Poor
#> 103        101 Streptomycin            2          0      F             3_Poor
#> 104        104 Streptomycin            2          0      M             3_Poor
#> 105        105 Streptomycin            2          0      F             3_Poor
#> 106        106 Streptomycin            2          0      F             3_Poor
#> 107        107 Streptomycin            2          0      F             3_Poor
#>                          baseline_temp baseline_esr baseline_cavitation
#> 1                      1_<=98.9F/37.2C      2_11-20                 yes
#> 2              3_100-100.9F/37.8-38.2C      2_11-20                  no
#> 3                      1_<=98.9F/37.2C      3_21-50                  no
#> 4                      1_<=98.9F/37.2C      3_21-50                  no
#> 5                2_99-99.9F/37.3-37.7C      3_21-50                  no
#> 6              3_100-100.9F/37.8-38.2C      3_21-50                  no
#> 7                2_99-99.9F/37.3-37.7C      3_21-50                 yes
#> 8                2_99-99.9F/37.3-37.7C      3_21-50                 yes
#> 9                2_99-99.9F/37.3-37.7C      3_21-50                 yes
#> 10                      4_>=101F/38.3C      3_21-50                 yes
#> 11             3_100-100.9F/37.8-38.2C      3_21-50                  no
#> 12               2_99-99.9F/37.3-37.7C      3_21-50                 yes
#> 13               2_99-99.9F/37.3-37.7C      3_21-50                 yes
#> 14                      4_>=101F/38.3C      3_21-50                 yes
#> 15               2_99-99.9F/37.3-37.7C      3_21-50                 yes
#> 16             3_100-100.9F/37.8-38.2C      3_21-50                  no
#> 17             3_100-100.9F/37.8-38.2C      3_21-50                  no
#> 18                     1_<=98.9F/37.2C      3_21-50                  no
#> 19             3_100-100.9F/37.8-38.2C      3_21-50                  no
#> 20             3_100-100.9F/37.8-38.2C      3_21-50                  no
#> 21             3_100-100.9F/37.8-38.2C      3_21-50                  no
#> 22             3_100-100.9F/37.8-38.2C      3_21-50                  no
#> 23               2_99-99.9F/37.3-37.7C        4_51+                 yes
#> 24               2_99-99.9F/37.3-37.7C        4_51+                  no
#> 25               2_99-99.9F/37.3-37.7C        4_51+                  no
#> 26             3_100-100.9F/37.8-38.2C        4_51+                 yes
#> 27             3_100-100.9F/37.8-38.2C        4_51+                 yes
#> 28             3_100-100.9F/37.8-38.2C        4_51+                 yes
#> 29               2_99-99.9F/37.3-37.7C        4_51+                 yes
#> 30                      4_>=101F/38.3C        4_51+                 yes
#> 31                      4_>=101F/38.3C        4_51+                 yes
#> 32                      4_>=101F/38.3C        4_51+                 yes
#> 33                      4_>=101F/38.3C        4_51+                  no
#> 34                      4_>=101F/38.3C        4_51+                  no
#> 35                      4_>=101F/38.3C        4_51+                  no
#> 36                      4_>=101F/38.3C        4_51+                  no
#> 37                      4_>=101F/38.3C        4_51+                  no
#> 38                      4_>=101F/38.3C        4_51+                 yes
#> 39                      4_>=101F/38.3C        4_51+                  no
#> 40                      4_>=101F/38.3C        4_51+                 yes
#> 41             3_100-100.9F/37.8-38.2C        4_51+                  no
#> 42             3_100-100.9F/37.8-38.2C        4_51+                 yes
#> 43             3_100-100.9F/37.8-38.2C         <NA>                 yes
#> 44             3_100-100.9F/37.8-38.2C        4_51+                 yes
#> 45             3_100-100.9F/37.8-38.2C        4_51+                 yes
#> 46               2_99-99.9F/37.3-37.7C        4_51+                 yes
#> 47                      4_>=101F/38.3C        4_51+                 yes
#> 48                      4_>=101F/38.3C        4_51+                 yes
#> 49                      4_>=101F/38.3C        4_51+                 yes
#> 50                      4_>=101F/38.3C        4_51+                 yes
#> 51                      4_>=101F/38.3C        4_51+                 yes
#> 52                      4_>=101F/38.3C        4_51+                 yes
#> 53                      4_>=101F/38.3C      2_11-20                  no
#> 54                     1_<=98.9F/37.2C      2_11-20                  no
#> 55                     1_<=98.9F/37.2C      3_21-50                  no
#> 56                     1_<=98.9F/37.2C      3_21-50                  no
#> 57               2_99-99.9F/37.3-37.7C      3_21-50                  no
#> 58               2_99-99.9F/37.3-37.7C      3_21-50                  no
#> 59               2_99-99.9F/37.3-37.7C      3_21-50                  no
#> 60               2_99-99.9F/37.3-37.7C      3_21-50                  no
#> 61             3_100-100.9F/37.8-38.2C      3_21-50                  no
#> 62               2_99-99.9F/37.3-37.7C      2_11-20                  no
#> 63               2_99-99.9F/37.3-37.7C      3_21-50                  no
#> 64                      4_>=101F/38.3C      3_21-50                  no
#> 65             3_100-100.9F/37.8-38.2C        4_51+                  no
#> 66               2_99-99.9F/37.3-37.7C      3_21-50                  no
#> 67                      4_>=101F/38.3C      3_21-50                  no
#> 68    2_99-99.9F/37.3-37.7C/37.3-37.7C      3_21-50                  no
#> 69               2_99-99.9F/37.3-37.7C      3_21-50                  no
#> 70               2_99-99.9F/37.3-37.7C      3_21-50                  no
#> 71                      4_>=101F/38.3C      3_21-50                  no
#> 72                      4_>=101F/38.3C      3_21-50                  no
#> 73                      4_>=101F/38.3C        4_51+                  no
#> 74                      4_>=101F/38.3C        4_51+                  no
#> 75             3_100-100.9F/37.8-38.2C        4_51+                  no
#> 76             3_100-100.9F/37.8-38.2C        4_51+                 yes
#> 77             3_100-100.9F/37.8-38.2C        4_51+                 yes
#> 78             3_100-100.9F/37.8-38.2C        4_51+                 yes
#> 79             3_100-100.9F/37.8-38.2C        4_51+                 yes
#> 80             3_100-100.9F/37.8-38.2C        4_51+                 yes
#> 81                      4_>=101F/38.3C        4_51+                 yes
#> 82             3_100-100.9F/37.8-38.2C        4_51+                 yes
#> 83             3_100-100.9F/37.8-38.2C        4_51+                 yes
#> 84             3_100-100.9F/37.8-38.2C        4_51+                 yes
#> 85  3_100-100.9F/37.8-38.2C/37.8-38.2C        4_51+                 yes
#> 86             3_100-100.9F/37.8-38.2C        4_51+                 yes
#> 87             3_100-100.9F/37.8-38.2C        4_51+                 yes
#> 88                      4_>=101F/38.3C        4_51+                 yes
#> 89                      4_>=101F/38.3C        4_51+                 yes
#> 90                      4_>=101F/38.3C        4_51+                 yes
#> 91             3_100-100.9F/37.8-38.2C        4_51+                 yes
#> 92               2_99-99.9F/37.3-37.7C        4_51+                 yes
#> 93                      4_>=101F/38.3C        4_51+                 yes
#> 94                      4_>=101F/38.3C        4_51+                 yes
#> 95                      4_>=101F/38.3C        4_51+                 yes
#> 96                      4_>=101F/38.3C        4_51+                 yes
#> 97                      4_>=101F/38.3C        4_51+                 yes
#> 98               2_99-99.9F/37.3-37.7C        4_51+                 yes
#> 99                      4_>=101F/38.3C        4_51+                 yes
#> 100                     4_>=101F/38.3C        4_51+                 yes
#> 101                     4_>=101F/38.3C        4_51+                 yes
#> 102              2_99-99.9F/37.3-37.7C        4_51+                 yes
#> 103                     4_>=101F/38.3C        4_51+                 yes
#> 104                     4_>=101F/38.3C        4_51+                 yes
#> 105                     4_>=101F/38.3C        4_51+                 yes
#> 106                     4_>=101F/38.3C        4_51+                 yes
#> 107                     4_>=101F/38.3C        4_51+                 yes
#>     strep_resistance                radiologic_6m rad_num improved
#> 1         1_sens_0-8   6_Considerable_improvement       6     TRUE
#> 2         1_sens_0-8       5_Moderate_improvement       5     TRUE
#> 3         1_sens_0-8       5_Moderate_improvement       5     TRUE
#> 4         1_sens_0-8       5_Moderate_improvement       5     TRUE
#> 5         1_sens_0-8       5_Moderate_improvement       5     TRUE
#> 6         1_sens_0-8   6_Considerable_improvement       6     TRUE
#> 7         1_sens_0-8       5_Moderate_improvement       5     TRUE
#> 8         1_sens_0-8       5_Moderate_improvement       5     TRUE
#> 9         1_sens_0-8       5_Moderate_improvement       5     TRUE
#> 10        1_sens_0-8       5_Moderate_improvement       5     TRUE
#> 11        1_sens_0-8   6_Considerable_improvement       6     TRUE
#> 12        1_sens_0-8       5_Moderate_improvement       5     TRUE
#> 13        1_sens_0-8       5_Moderate_improvement       5     TRUE
#> 14        1_sens_0-8       5_Moderate_improvement       5     TRUE
#> 15        1_sens_0-8       5_Moderate_improvement       5     TRUE
#> 16        1_sens_0-8   6_Considerable_improvement       6     TRUE
#> 17        1_sens_0-8       5_Moderate_improvement       5     TRUE
#> 18        1_sens_0-8                  4_No_change       4    FALSE
#> 19        1_sens_0-8     3_Moderate_deterioration       3    FALSE
#> 20        1_sens_0-8     3_Moderate_deterioration       3    FALSE
#> 21        1_sens_0-8     3_Moderate_deterioration       3    FALSE
#> 22        1_sens_0-8     3_Moderate_deterioration       3    FALSE
#> 23        1_sens_0-8                  4_No_change       4    FALSE
#> 24        1_sens_0-8     3_Moderate_deterioration       3    FALSE
#> 25        1_sens_0-8     3_Moderate_deterioration       3    FALSE
#> 26        1_sens_0-8     3_Moderate_deterioration       3    FALSE
#> 27        1_sens_0-8     3_Moderate_deterioration       3    FALSE
#> 28        1_sens_0-8     3_Moderate_deterioration       3    FALSE
#> 29        1_sens_0-8                  4_No_change       4    FALSE
#> 30        1_sens_0-8     3_Moderate_deterioration       3    FALSE
#> 31        1_sens_0-8     3_Moderate_deterioration       3    FALSE
#> 32        1_sens_0-8     3_Moderate_deterioration       3    FALSE
#> 33        1_sens_0-8 2_Considerable_deterioration       2    FALSE
#> 34        1_sens_0-8 2_Considerable_deterioration       2    FALSE
#> 35        1_sens_0-8 2_Considerable_deterioration       2    FALSE
#> 36        1_sens_0-8 2_Considerable_deterioration       2    FALSE
#> 37        1_sens_0-8                      1_Death       1    FALSE
#> 38        1_sens_0-8 2_Considerable_deterioration       2    FALSE
#> 39        1_sens_0-8                      1_Death       1    FALSE
#> 40        1_sens_0-8 2_Considerable_deterioration       2    FALSE
#> 41        1_sens_0-8                      1_Death       1    FALSE
#> 42        1_sens_0-8                      1_Death       1    FALSE
#> 43        1_sens_0-8                      1_Death       1    FALSE
#> 44        1_sens_0-8                      1_Death       1    FALSE
#> 45        1_sens_0-8                      1_Death       1    FALSE
#> 46        1_sens_0-8                      1_Death       1    FALSE
#> 47        1_sens_0-8                      1_Death       1    FALSE
#> 48        1_sens_0-8                      1_Death       1    FALSE
#> 49        1_sens_0-8                      1_Death       1    FALSE
#> 50        1_sens_0-8                      1_Death       1    FALSE
#> 51        1_sens_0-8                      1_Death       1    FALSE
#> 52        1_sens_0-8                      1_Death       1    FALSE
#> 53        1_sens_0-8   6_Considerable_improvement       6     TRUE
#> 54        1_sens_0-8   6_Considerable_improvement       6     TRUE
#> 55        1_sens_0-8   6_Considerable_improvement       6     TRUE
#> 56        1_sens_0-8   6_Considerable_improvement       6     TRUE
#> 57        1_sens_0-8   6_Considerable_improvement       6     TRUE
#> 58        1_sens_0-8   6_Considerable_improvement       6     TRUE
#> 59        1_sens_0-8   6_Considerable_improvement       6     TRUE
#> 60        1_sens_0-8   6_Considerable_improvement       6     TRUE
#> 61        1_sens_0-8   6_Considerable_improvement       6     TRUE
#> 62        2_mod_8-99   6_Considerable_improvement       6     TRUE
#> 63        2_mod_8-99   6_Considerable_improvement       6     TRUE
#> 64        2_mod_8-99   6_Considerable_improvement       6     TRUE
#> 65        2_mod_8-99       5_Moderate_improvement       5     TRUE
#> 66     3_resist_100+ 2_Considerable_deterioration       2    FALSE
#> 67     3_resist_100+ 2_Considerable_deterioration       2    FALSE
#> 68     3_resist_100+   6_Considerable_improvement       6     TRUE
#> 69     3_resist_100+       5_Moderate_improvement       5     TRUE
#> 70     3_resist_100+   6_Considerable_improvement       6     TRUE
#> 71     3_resist_100+     3_Moderate_deterioration       3    FALSE
#> 72     3_resist_100+   6_Considerable_improvement       6     TRUE
#> 73     3_resist_100+   6_Considerable_improvement       6     TRUE
#> 74     3_resist_100+       5_Moderate_improvement       5     TRUE
#> 75     3_resist_100+   6_Considerable_improvement       6     TRUE
#> 76        1_sens_0-8   6_Considerable_improvement       6     TRUE
#> 77        1_sens_0-8       5_Moderate_improvement       5     TRUE
#> 78        2_mod_8-99   6_Considerable_improvement       6     TRUE
#> 79        2_mod_8-99       5_Moderate_improvement       5     TRUE
#> 80     3_resist_100+   6_Considerable_improvement       6     TRUE
#> 81     3_resist_100+                      1_Death       1    FALSE
#> 82     3_resist_100+       5_Moderate_improvement       5     TRUE
#> 83     3_resist_100+   6_Considerable_improvement       6     TRUE
#> 84     3_resist_100+     3_Moderate_deterioration       3    FALSE
#> 85     3_resist_100+   6_Considerable_improvement       6     TRUE
#> 86     3_resist_100+   6_Considerable_improvement       6     TRUE
#> 87     3_resist_100+   6_Considerable_improvement       6     TRUE
#> 88     3_resist_100+                      1_Death       1    FALSE
#> 89     3_resist_100+                  4_No_change       4    FALSE
#> 90     3_resist_100+     3_Moderate_deterioration       3    FALSE
#> 91     3_resist_100+     3_Moderate_deterioration       3    FALSE
#> 92        1_sens_0-8 2_Considerable_deterioration       2    FALSE
#> 93        1_sens_0-8   6_Considerable_improvement       6     TRUE
#> 94        2_mod_8-99   6_Considerable_improvement       6     TRUE
#> 95        2_mod_8-99       5_Moderate_improvement       5     TRUE
#> 96     3_resist_100+   6_Considerable_improvement       6     TRUE
#> 97     3_resist_100+       5_Moderate_improvement       5     TRUE
#> 98     3_resist_100+ 2_Considerable_deterioration       2    FALSE
#> 99     3_resist_100+       5_Moderate_improvement       5     TRUE
#> 100    3_resist_100+     3_Moderate_deterioration       3    FALSE
#> 101    3_resist_100+                      1_Death       1    FALSE
#> 102    3_resist_100+                  4_No_change       4    FALSE
#> 103    3_resist_100+ 2_Considerable_deterioration       2    FALSE
#> 104    3_resist_100+       5_Moderate_improvement       5     TRUE
#> 105    3_resist_100+ 2_Considerable_deterioration       2    FALSE
#> 106    3_resist_100+                      1_Death       1    FALSE
#> 107    3_resist_100+   6_Considerable_improvement       6     TRUE

Challenge

Now try to do this with the indo_rct dataset, using the treatment variable group and the outcome variable of outcome. Add a total row, percentages, and a title.

indo_rct 
#> # A tibble: 602 × 33
#>       id site    age  risk gender   outcome sod   pep   recpanc psphinc precut
#>    <dbl> <fct> <dbl> <dbl> <fct>    <fct>   <fct> <fct> <fct>   <fct>   <fct> 
#>  1  1001 1_UM     26   2   1_female 1_yes   1_yes 0_no  1_yes   0_no    0_no  
#>  2  1002 1_UM     24   1   2_male   0_no    0_no  1_yes 0_no    0_no    0_no  
#>  3  1003 1_UM     57   1   1_female 0_no    1_yes 0_no  0_no    0_no    0_no  
#>  4  1004 1_UM     29   2   1_female 1_yes   1_yes 0_no  0_no    0_no    0_no  
#>  5  1005 1_UM     38   3.5 1_female 0_no    1_yes 1_yes 0_no    1_yes   0_no  
#>  6  1006 1_UM     59   3   1_female 0_no    1_yes 0_no  0_no    0_no    1_yes 
#>  7  1007 1_UM     60   1.5 1_female 0_no    0_no  0_no  1_yes   0_no    0_no  
#>  8  1008 1_UM     29   1   2_male   0_no    0_no  0_no  0_no    0_no    0_no  
#>  9  1009 1_UM     53   2   2_male   0_no    0_no  0_no  1_yes   0_no    0_no  
#> 10  1010 1_UM     20   2   2_male   0_no    0_no  0_no  0_no    0_no    1_yes 
#> # ℹ 592 more rows
#> # ℹ 22 more variables: difcan <fct>, pneudil <fct>, amp <fct>, paninj <fct>,
#> #   acinar <fct>, brush <fct>, asa81 <fct>, asa325 <fct>, asa <fct>,
#> #   prophystent <fct>, therastent <fct>, pdstent <fct>, sodsom <fct>,
#> #   bsphinc <fct>, bstent <fct>, chole <fct>, pbmal <fct>, train <fct>,
#> #   status <fct>, type <fct>, rx <fct>, bleed <dbl>