Update in R package moonBook

Keon-Woong Moon

2018-07-23

Function “mytable”

Function “mytable”" produce table for descriptive analysis easily. It is most useful to make table to describe baseline charateristics common in medical research papers.

Basic Usage

If you are unfamiliar to package moonBook and mytable function, please see the R package moonBook vignette at: https://CRAN.R-project.org/package=moonBook/vignettes/moonBook.html

Explore a data.frame

You can use mytable function to explore a data.frame.

require(moonBook)
require(ztable)
require(magrittr)
options(ztable.type="html")

mytable(acs)

         Descriptive Statistics        
---------------------------------------- 
                        N      Total    
---------------------------------------- 
 age                  857  63.3 ± 11.7 
 sex                  857               
   - Female                287  (33.5%) 
   - Male                  570  (66.5%) 
 cardiogenicShock     857               
   - No                    805  (93.9%) 
   - Yes                     52  (6.1%) 
 entry                857               
   - Femoral               312  (36.4%) 
   - Radial                545  (63.6%) 
 Dx                   857               
   - NSTEMI                153  (17.9%) 
   - STEMI                 304  (35.5%) 
   - Unstable Angina       400  (46.7%) 
 EF                   723   55.8 ± 9.6 
 height               764  163.2 ± 9.1 
 weight               766  64.8 ± 11.4 
 BMI                  764   24.3 ± 3.3 
 obesity              857               
   - No                    567  (66.2%) 
   - Yes                   290  (33.8%) 
 TC                   834 185.2 ± 47.8 
 LDLC                 833 116.6 ± 41.1 
 HDLC                 834  38.2 ± 11.1 
 TG                   842 125.2 ± 90.9 
 DM                   857               
   - No                    553  (64.5%) 
   - Yes                   304  (35.5%) 
 HBP                  857               
   - No                    356  (41.5%) 
   - Yes                   501  (58.5%) 
 smoking              857               
   - Ex-smoker             204  (23.8%) 
   - Never                 332  (38.7%) 
   - Smoker                321  (37.5%) 
---------------------------------------- 

You can use formula without grouping variable(s).

mytable(~age+sex,data=acs)

    Descriptive Statistics    
------------------------------- 
               N      Total    
------------------------------- 
 age         857  63.3 ± 11.7 
 sex         857               
   - Female       287  (33.5%) 
   - Male         570  (66.5%) 
------------------------------- 

Compress an object of class mytable

You can compress mytable. If rows dealing with categorical variables have two unique values, it can be printed in a single row rather than three rows.

mytable(Dx~sex,data=acs)

             Descriptive Statistics by 'Dx'             
_________________________________________________________ 
              NSTEMI       STEMI    Unstable Angina   p  
              (N=153)     (N=304)       (N=400)    
--------------------------------------------------------- 
 sex                                                0.012
   - Female 50 (32.7%)  84 (27.6%)    153 (38.2%)        
   - Male   103 (67.3%) 220 (72.4%)   247 (61.8%)        
--------------------------------------------------------- 
mytable(Dx~sex,data=acs) %>% compress

            Descriptive Statistics by 'Dx'            
_______________________________________________________ 
            NSTEMI       STEMI    Unstable Angina   p  
            (N=153)     (N=304)       (N=400)    
------------------------------------------------------- 
 sex:Male 103 (67.3%) 220 (72.4%)   247 (61.8%)   0.012
------------------------------------------------------- 

The default representative group is the second group. If you want the first group to being representative group, please use the no argument.

mytable(Dx~sex,data=acs) %>% compress(no=1)

            Descriptive Statistics by 'Dx'            
_______________________________________________________ 
              NSTEMI     STEMI    Unstable Angina   p  
             (N=153)    (N=304)       (N=400)    
------------------------------------------------------- 
 sex:Female 50 (32.7%) 84 (27.6%)   153 (38.2%)   0.012
------------------------------------------------------- 

Sometimes it is more simple to omit the represntative group name. You can do this by set the add.label argument FALSE.

mytable(Dx~cardiogenicShock+DM+obesity+HBP,data=acs) %>% compress

                  Descriptive Statistics by 'Dx'                 
__________________________________________________________________ 
                        NSTEMI      STEMI    Unstable Angina   p  
                       (N=153)     (N=304)       (N=400)    
------------------------------------------------------------------ 
 cardiogenicShock:Yes 4 ( 2.6%)  48 (15.8%)     0 ( 0.0%)    0.000
 DM:Yes               57 (37.3%) 96 (31.6%)    151 (37.8%)   0.209
 obesity:Yes          47 (30.7%) 95 (31.2%)    148 (37.0%)   0.186
 HBP:Yes              91 (59.5%) 154 (50.7%)   256 (64.0%)   0.002
------------------------------------------------------------------ 
mytable(Dx~cardiogenicShock+DM+obesity+HBP,data=acs) %>% compress(add.label=FALSE)

                Descriptive Statistics by 'Dx'               
______________________________________________________________ 
                    NSTEMI      STEMI    Unstable Angina   p  
                   (N=153)     (N=304)       (N=400)    
-------------------------------------------------------------- 
 cardiogenicShock 4 ( 2.6%)  48 (15.8%)     0 ( 0.0%)    0.000
 DM               57 (37.3%) 96 (31.6%)    151 (37.8%)   0.209
 obesity          47 (30.7%) 95 (31.2%)    148 (37.0%)   0.186
 HBP              91 (59.5%) 154 (50.7%)   256 (64.0%)   0.002
-------------------------------------------------------------- 

You can print mytable object in ‘html5’ or ‘LaTex’ format with ztable.

mytable(Dx~cardiogenicShock+DM+obesity+HBP,data=acs) %>% compress(add.label=FALSE) %>% ztable
NSTEMI STEMI Unstable Angina p
(N=153) (N=304) (N=400)
cardiogenicShock 4 ( 2.6%) 48 (15.8%) 0 ( 0.0%) < 0.001
DM 57 (37.3%) 96 (31.6%) 151 (37.8%) 0.209
obesity 47 (30.7%) 95 (31.2%) 148 (37.0%) 0.186
HBP 91 (59.5%) 154 (50.7%) 256 (64.0%) 0.002

Delete Rows of an object of class mytable

You can delete rows of an obect of class mytable.

mytable(sex~Dx,data=acs)

         Descriptive Statistics by 'sex'         
__________________________________________________ 
                       Female       Male       p  
                       (N=287)     (N=570)  
-------------------------------------------------- 
 Dx                                          0.012
   - NSTEMI          50 (17.4%)  103 (18.1%)      
   - STEMI           84 (29.3%)  220 (38.6%)      
   - Unstable Angina 153 (53.3%) 247 (43.3%)      
-------------------------------------------------- 

If you want to delete the second row, use the deleteRows() function.

mytable(sex~Dx,data=acs) %>% deleteRows(2)

         Descriptive Statistics by 'sex'         
__________________________________________________ 
                       Female       Male       p  
                       (N=287)     (N=570)  
-------------------------------------------------- 
 Dx                                          0.012
   - STEMI           84 (29.3%)  220 (38.6%)      
   - Unstable Angina 153 (53.3%) 247 (43.3%)      
-------------------------------------------------- 

You can delete rows of an object of class cbind.mytable.

mytable(sex+HBP~age+Dx,data=acs) %>% deleteRows(3)

              Descriptive Statistics stratified by 'sex' and 'HBP'             
________________________________________________________________________________ 
                                   Male                          Female            
                     ----------------------------- ----------------------------- 
                         No          Yes       p       No          Yes       p  
                       (N=273)     (N=297)          (N=83)      (N=204)       
-------------------------------------------------------------------------------- 
 age                 57.5 ± 11.3 63.5 ± 10.4 0.000 68.0 ± 12.3 69.0 ± 10.0 0.526
 Dx                                          0.016                         0.084
   - STEMI           122 (44.7%) 98 (33.0%)        28 (33.7%)  56 (27.5%)       
   - Unstable Angina 108 (39.6%) 139 (46.8%)       36 (43.4%)  117 (57.4%)      
-------------------------------------------------------------------------------- 

Methods for categorical variables

You can select method for categorical variables with catMethod argument. Possible values are :

You can see which tests are used if you set show.all argument of mytable TRUE.

mytable(obesity~HBP,data=acs,catMethod=1,show.all=TRUE)

                                Descriptive Statistics by 'obesity'                                
____________________________________________________________________________________________________ 
             No          Yes       p   sig  p1   p2  p3      class              ptest             N 
           (N=567)     (N=290)  
---------------------------------------------------------------------------------------------------- 
 HBP                             0.034 **  0.034    2.000 categorical Pearson's Chi-squared test 857
   - No  250 (44.1%) 106 (36.6%)                                                                    
   - Yes 317 (55.9%) 184 (63.4%)                                                                    
---------------------------------------------------------------------------------------------------- 

For formatted numbers: addComma()

Sometimes, you want to display formatted numbers. For example, 1234.5 can be printed as 1,234.5. You can do this using addComma() function

data(diamonds,package="ggplot2")
mytable(diamonds) %>% addComma

          Descriptive Statistics         
------------------------------------------ 
                   N           Total      
------------------------------------------ 
 carat          53,940         0.8 ± 0.5 
 cut            53,940                    
   - Fair                      1,610 (3%) 
   - Good                    4,906 (9.1%) 
   - Very Good             12,082 (22.4%) 
   - Premium               13,791 (25.6%) 
   - Ideal                   21,551 (40%) 
 color          53,940                    
   - D                      6,775 (12.6%) 
   - E                      9,797 (18.2%) 
   - F                      9,542 (17.7%) 
   - G                     11,292 (20.9%) 
   - H                      8,304 (15.4%) 
   - I                      5,422 (10.1%) 
   - J                       2,808 (5.2%) 
 clarity        53,940                    
   - I1                        741 (1.4%) 
   - SI2                      9,194 (17%) 
   - SI1                   13,065 (24.2%) 
   - VS2                   12,258 (22.7%) 
   - VS1                    8,171 (15.1%) 
   - VVS2                    5,066 (9.4%) 
   - VVS1                    3,655 (6.8%) 
   - IF                      1,790 (3.3%) 
 depth          53,940        61.7 ± 1.4 
 table          53,940        57.5 ± 2.2 
 price          53,940 3,932.8 ± 3,989.4 
 x              53,940         5.7 ± 1.1 
 y              53,940         5.7 ± 1.1 
 z              53,940         3.5 ± 0.7 
------------------------------------------ 

Also you can print this in ‘html5’ or ‘LaTex’ format with ztable.

mytable(cut~.,data=diamonds) %>% addComma %>% ztable
Fair Good Very Good Premium Ideal p
(N= 1,610) (N= 4,906) (N=12,082) (N=13,791) (N=21,551)
carat 1.0 ± 0.5 0.8 ± 0.5 0.8 ± 0.5 0.9 ± 0.5 0.7 ± 0.4 < 0.001
color < 0.001
    D 163 (10.1%) 662 (13.5%) 1,513 (12.5%) 1,603 (11.6%) 2,834 (13.2%)
    E 224 (13.9%) 933 (19%) 2,400 (19.9%) 2,337 (16.9%) 3,903 (18.1%)
    F 312 (19.4%) 909 (18.5%) 2,164 (17.9%) 2,331 (16.9%) 3,826 (17.8%)
    G 314 (19.5%) 871 (17.8%) 2,299 (19%) 2,924 (21.2%) 4,884 (22.7%)
    H 303 (18.8%) 702 (14.3%) 1,824 (15.1%) 2,360 (17.1%) 3,115 (14.5%)
    I 175 (10.9%) 522 (10.6%) 1,204 (10%) 1,428 (10.4%) 2,093 (9.7%)
    J 119 (7.4%) 307 (6.3%) 678 (5.6%) 808 (5.9%) 896 (4.2%)
clarity < 0.001
    I1 210 (13%) 96 (2%) 84 (0.7%) 205 (1.5%) 146 (0.7%)
    SI2 466 (28.9%) 1,081 (22%) 2,100 (17.4%) 2,949 (21.4%) 2,598 (12.1%)
    SI1 408 (25.3%) 1,560 (31.8%) 3,240 (26.8%) 3,575 (25.9%) 4,282 (19.9%)
    VS2 261 (16.2%) 978 (19.9%) 2,591 (21.4%) 3,357 (24.3%) 5,071 (23.5%)
    VS1 170 (10.6%) 648 (13.2%) 1,775 (14.7%) 1,989 (14.4%) 3,589 (16.7%)
    VVS2 69 (4.3%) 286 (5.8%) 1,235 (10.2%) 870 (6.3%) 2,606 (12.1%)
    VVS1 17 (1.1%) 186 (3.8%) 789 (6.5%) 616 (4.5%) 2,047 (9.5%)
    IF 9 (0.6%) 71 (1.4%) 268 (2.2%) 230 (1.7%) 1,212 (5.6%)
depth 64.0 ± 3.6 62.4 ± 2.2 61.8 ± 1.4 61.3 ± 1.2 61.7 ± 0.7 < 0.001
table 59.1 ± 3.9 58.7 ± 2.9 58.0 ± 2.1 58.7 ± 1.5 56.0 ± 1.2 < 0.001
price 4,358.8 ± 3,560.4 3,928.9 ± 3,681.6 3,981.8 ± 3,935.9 4,584.3 ± 4,349.2 3,457.5 ± 3,808.4 < 0.001
x 6.2 ± 1.0 5.8 ± 1.1 5.7 ± 1.1 6.0 ± 1.2 5.5 ± 1.1 < 0.001
z 4.0 ± 0.7 3.6 ± 0.7 3.6 ± 0.7 3.6 ± 0.7 3.4 ± 0.7 < 0.001