cramLevels feature test

References

Load packages

### Load dataset
library(survival)
data(pbc)
head(pbc)
##   id time status trt   age sex ascites hepato spiders edema bili chol albumin copper alk.phos    ast trig platelet
## 1  1  400      2   1 58.77   f       1      1       1   1.0 14.5  261    2.60    156     1718 137.95  172      190
## 2  2 4500      0   1 56.45   f       0      1       1   0.0  1.1  302    4.14     54     7395 113.52   88      221
## 3  3 1012      2   1 70.07   m       0      0       0   0.5  1.4  176    3.48    210      516  96.10   55      151
## 4  4 1925      2   1 54.74   f       0      1       1   0.5  1.8  244    2.54     64     6122  60.63   92      183
## 5  5 1504      1   2 38.11   f       0      1       1   0.0  3.4  279    3.53    143      671 113.15   72      136
## 6  6 2503      2   2 66.26   f       0      1       0   0.0  0.8  248    3.98     50      944  93.00   63       NA
##   protime stage
## 1    12.2     4
## 2    10.6     3
## 3    12.0     4
## 4    10.3     4
## 5    10.9     3
## 6    11.0     3

## Define variables to convert to factors
factorVars <- c("status","trt","ascites","hepato","spiders","edema","stage")
pbc[factorVars] <- lapply(pbc[factorVars], factor)

##  Define variables to analyze
vars <- c("time", "trt", "age", "sex", "ascites", "hepato",
          "spiders", "edema", "bili", "chol", "albumin", "copper", "alk.phos",
          "ast", "trig", "platelet", "protime", "stage")

## Strata
strata <- "status"
strataTwoVars <- c("status","sex")
strataThreeVars <- c("sex","ascites","hepato")

## Data and variables
dataFrame <- pbc
exactVars <- c("ascites","edema")

## Create edema strata that are of sizes 1, 2, and 3
smallDataFrame <- pbc[c(2, 3,4, 1,10,14),]
smallStrata <- "edema"
## Load the latest dev repo
library(devtools)
load_all("~/Documents/statistics/package_development/tableone/")

Test

### Define variables
nonNormalVars <- c("bili","chol","copper","alk.phos")

### Overall
tableOne <- CreateTableOne(vars = vars, data = dataFrame)
## Print method
print(tableOne)
##                       
##                        Overall          
##   n                    418              
##   time (mean (sd))     1917.78 (1104.67)
##   trt = 2 (%)              154 (49.4)   
##   age (mean (sd))        50.74 (10.45)  
##   sex = f (%)              374 (89.5)   
##   ascites = 1 (%)           24 (7.7)    
##   hepato = 1 (%)           160 (51.3)   
##   spiders = 1 (%)           90 (28.8)   
##   edema (%)                             
##      0                     354 (84.7)   
##      0.5                    44 (10.5)   
##      1                      20 ( 4.8)   
##   bili (mean (sd))        3.22 (4.41)   
##   chol (mean (sd))      369.51 (231.94) 
##   albumin (mean (sd))     3.50 (0.42)   
##   copper (mean (sd))     97.65 (85.61)  
##   alk.phos (mean (sd)) 1982.66 (2140.39)
##   ast (mean (sd))       122.56 (56.70)  
##   trig (mean (sd))      124.70 (65.15)  
##   platelet (mean (sd))  257.02 (98.33)  
##   protime (mean (sd))    10.73 (1.02)   
##   stage (%)                             
##      1                      21 ( 5.1)   
##      2                      92 (22.3)   
##      3                     155 (37.6)   
##      4                     144 (35.0)

### Cramming test
## Cram sex
print(tableOne, cramVars = c("sex","hepato"))
##                       
##                        Overall             
##   n                    418                 
##   time (mean (sd))     1917.78 (1104.67)   
##   trt = 2 (%)              154 (49.4)      
##   age (mean (sd))        50.74 (10.45)     
##   sex = m/f (%)         44/374 (10.5/89.5) 
##   ascites = 1 (%)           24 (7.7)       
##   hepato = 0/1 (%)     152/160 (48.7/51.3) 
##   spiders = 1 (%)           90 (28.8)      
##   edema (%)                                
##      0                     354 (84.7)      
##      0.5                    44 (10.5)      
##      1                      20 ( 4.8)      
##   bili (mean (sd))        3.22 (4.41)      
##   chol (mean (sd))      369.51 (231.94)    
##   albumin (mean (sd))     3.50 (0.42)      
##   copper (mean (sd))     97.65 (85.61)     
##   alk.phos (mean (sd)) 1982.66 (2140.39)   
##   ast (mean (sd))       122.56 (56.70)     
##   trig (mean (sd))      124.70 (65.15)     
##   platelet (mean (sd))  257.02 (98.33)     
##   protime (mean (sd))    10.73 (1.02)      
##   stage (%)                                
##      1                      21 ( 5.1)      
##      2                      92 (22.3)      
##      3                     155 (37.6)      
##      4                     144 (35.0)
print(tableOne, format = "f", cramVars = c("sex","hepato"))
##                       
##                        Overall          
##   n                    418              
##   time (mean (sd))     1917.78 (1104.67)
##   trt = 2                  154          
##   age (mean (sd))        50.74 (10.45)  
##   sex = m/f             44/374          
##   ascites = 1               24          
##   hepato = 0/1         152/160          
##   spiders = 1               90          
##   edema                                 
##      0                     354          
##      0.5                    44          
##      1                      20          
##   bili (mean (sd))        3.22 (4.41)   
##   chol (mean (sd))      369.51 (231.94) 
##   albumin (mean (sd))     3.50 (0.42)   
##   copper (mean (sd))     97.65 (85.61)  
##   alk.phos (mean (sd)) 1982.66 (2140.39)
##   ast (mean (sd))       122.56 (56.70)  
##   trig (mean (sd))      124.70 (65.15)  
##   platelet (mean (sd))  257.02 (98.33)  
##   protime (mean (sd))    10.73 (1.02)   
##   stage                                 
##      1                      21          
##      2                      92          
##      3                     155          
##      4                     144
print(tableOne, format = "p", cramVars = c("sex","hepato"))
##                       
##                        Overall            
##   n                      418              
##   time (mean (sd))       1917.78 (1104.67)
##   trt = 2 (%)               49.4          
##   age (mean (sd))          50.74 (10.45)  
##   sex = m/f (%)        10.5/89.5          
##   ascites = 1 (%)            7.7          
##   hepato = 0/1 (%)     48.7/51.3          
##   spiders = 1 (%)           28.8          
##   edema (%)                               
##      0                      84.7          
##      0.5                    10.5          
##      1                       4.8          
##   bili (mean (sd))          3.22 (4.41)   
##   chol (mean (sd))        369.51 (231.94) 
##   albumin (mean (sd))       3.50 (0.42)   
##   copper (mean (sd))       97.65 (85.61)  
##   alk.phos (mean (sd))   1982.66 (2140.39)
##   ast (mean (sd))         122.56 (56.70)  
##   trig (mean (sd))        124.70 (65.15)  
##   platelet (mean (sd))    257.02 (98.33)  
##   protime (mean (sd))      10.73 (1.02)   
##   stage (%)                               
##      1                       5.1          
##      2                      22.3          
##      3                      37.6          
##      4                      35.0
print(tableOne, format = "pf", cramVars = c("sex","hepato"))
##                        
##                         Overall             
##   n                       418               
##   time (mean (sd))        1917.78 (1104.67) 
##   trt = 2 % (freq)           49.4 (154)     
##   age (mean (sd))           50.74 (10.45)   
##   sex = m/f % (freq)    10.5/89.5 (44/374)  
##   ascites = 1 % (freq)        7.7 (24)      
##   hepato = 0/1 % (freq) 48.7/51.3 (152/160) 
##   spiders = 1 % (freq)       28.8 (90)      
##   edema % (freq)                            
##      0                       84.7 (354)     
##      0.5                     10.5 ( 44)     
##      1                        4.8 ( 20)     
##   bili (mean (sd))           3.22 (4.41)    
##   chol (mean (sd))         369.51 (231.94)  
##   albumin (mean (sd))        3.50 (0.42)    
##   copper (mean (sd))        97.65 (85.61)   
##   alk.phos (mean (sd))    1982.66 (2140.39) 
##   ast (mean (sd))          122.56 (56.70)   
##   trig (mean (sd))         124.70 (65.15)   
##   platelet (mean (sd))     257.02 (98.33)   
##   protime (mean (sd))       10.73 (1.02)    
##   stage % (freq)                            
##      1                        5.1 ( 21)     
##      2                       22.3 ( 92)     
##      3                       37.6 (155)     
##      4                       35.0 (144)




## Summary method
summary(tableOne)
## 
##      ### Summary of continuous variables ###
## 
## strata: Overall
##            n miss mean     sd median    p25  p75   min   max  skew kurt
## time     418    0 1918 1104.7   1730 1092.8 2614  41.0  4795  0.47 -0.5
## age      418    0   51   10.4     51   42.8   58  26.3    78  0.09 -0.6
## bili     418    0    3    4.4      1    0.8    3   0.3    28  2.72  8.1
## chol     418  134  370  231.9    310  249.5  400 120.0  1775  3.41 14.3
## albumin  418    0    3    0.4      4    3.2    4   2.0     5 -0.47  0.6
## copper   418  108   98   85.6     73   41.2  123   4.0   588  2.30  7.6
## alk.phos 418  106 1983 2140.4   1259  871.5 1980 289.0 13862  2.99  9.7
## ast      418  106  123   56.7    115   80.6  152  26.4   457  1.45  4.3
## trig     418  136  125   65.1    108   84.2  151  33.0   598  2.52 11.8
## platelet 418   11  257   98.3    251  188.5  318  62.0   721  0.63  0.9
## protime  418    2   11    1.0     11   10.0   11   9.0    18  2.22 10.0
## 
## =======================================================================================
## 
##      ### Summary of categorical variables ### 
## 
## strata: Overall
##      var   n miss level freq percent cum.percent
##      trt 418  106     1  158    50.6        50.6
##                       2  154    49.4       100.0
##                                                 
##      sex 418    0     m   44    10.5        10.5
##                       f  374    89.5       100.0
##                                                 
##  ascites 418  106     0  288    92.3        92.3
##                       1   24     7.7       100.0
##                                                 
##   hepato 418  106     0  152    48.7        48.7
##                       1  160    51.3       100.0
##                                                 
##  spiders 418  106     0  222    71.2        71.2
##                       1   90    28.8       100.0
##                                                 
##    edema 418    0     0  354    84.7        84.7
##                     0.5   44    10.5        95.2
##                       1   20     4.8       100.0
##                                                 
##    stage 418    6     1   21     5.1         5.1
##                       2   92    22.3        27.4
##                       3  155    37.6        65.0
##                       4  144    35.0       100.0
## 
## quote
print(tableOne, quote = T)
##                         ""
##  ""                      "Overall"          
##   "n"                    "418"              
##   "time (mean (sd))"     "1917.78 (1104.67)"
##   "trt = 2 (%)"          "    154 (49.4) "  
##   "age (mean (sd))"      "  50.74 (10.45)"  
##   "sex = f (%)"          "    374 (89.5) "  
##   "ascites = 1 (%)"      "     24 (7.7) "   
##   "hepato = 1 (%)"       "    160 (51.3) "  
##   "spiders = 1 (%)"      "     90 (28.8) "  
##   "edema (%)"            "    "             
##   "   0"                 "    354 (84.7) "  
##   "   0.5"               "     44 (10.5) "  
##   "   1"                 "     20 ( 4.8) "  
##   "bili (mean (sd))"     "   3.22 (4.41)"   
##   "chol (mean (sd))"     " 369.51 (231.94)" 
##   "albumin (mean (sd))"  "   3.50 (0.42)"   
##   "copper (mean (sd))"   "  97.65 (85.61)"  
##   "alk.phos (mean (sd))" "1982.66 (2140.39)"
##   "ast (mean (sd))"      " 122.56 (56.70)"  
##   "trig (mean (sd))"     " 124.70 (65.15)"  
##   "platelet (mean (sd))" " 257.02 (98.33)"  
##   "protime (mean (sd))"  "  10.73 (1.02)"   
##   "stage (%)"            "    "             
##   "   1"                 "     21 ( 5.1) "  
##   "   2"                 "     92 (22.3) "  
##   "   3"                 "    155 (37.6) "  
##   "   4"                 "    144 (35.0) "
## Digits
print(tableOne , catDigits = 2, contDigits = 5)
##                       
##                        Overall                
##   n                    418                    
##   time (mean (sd))     1917.78230 (1104.67299)
##   trt = 2 (%)                 154 (49.36)     
##   age (mean (sd))        50.74155 (10.44721)  
##   sex = f (%)                 374 (89.47)     
##   ascites = 1 (%)              24 (7.69)      
##   hepato = 1 (%)              160 (51.28)     
##   spiders = 1 (%)              90 (28.85)     
##   edema (%)                                   
##      0                        354 (84.69)     
##      0.5                       44 (10.53)     
##      1                         20 ( 4.78)     
##   bili (mean (sd))        3.22081 (4.40751)   
##   chol (mean (sd))      369.51056 (231.94455) 
##   albumin (mean (sd))     3.49744 (0.42497)   
##   copper (mean (sd))     97.64839 (85.61392)  
##   alk.phos (mean (sd)) 1982.65577 (2140.38882)
##   ast (mean (sd))       122.55635 (56.69952)  
##   trig (mean (sd))      124.70213 (65.14864)  
##   platelet (mean (sd))  257.02457 (98.32558)  
##   protime (mean (sd))    10.73173 (1.02200)   
##   stage (%)                                   
##      1                         21 ( 5.10)     
##      2                         92 (22.33)     
##      3                        155 (37.62)     
##      4                        144 (34.95)
## median [IQR]
print(tableOne, nonnormal = nonNormalVars)
##                          
##                           Overall                  
##   n                       418                      
##   time (mean (sd))        1917.78 (1104.67)        
##   trt = 2 (%)                 154 (49.4)           
##   age (mean (sd))           50.74 (10.45)          
##   sex = f (%)                 374 (89.5)           
##   ascites = 1 (%)              24 (7.7)            
##   hepato = 1 (%)              160 (51.3)           
##   spiders = 1 (%)              90 (28.8)           
##   edema (%)                                        
##      0                        354 (84.7)           
##      0.5                       44 (10.5)           
##      1                         20 ( 4.8)           
##   bili (median [IQR])        1.40 [0.80, 3.40]     
##   chol (median [IQR])      309.50 [249.50, 400.00] 
##   albumin (mean (sd))        3.50 (0.42)           
##   copper (median [IQR])     73.00 [41.25, 123.00]  
##   alk.phos (median [IQR]) 1259.00 [871.50, 1980.00]
##   ast (mean (sd))          122.56 (56.70)          
##   trig (mean (sd))         124.70 (65.15)          
##   platelet (mean (sd))     257.02 (98.33)          
##   protime (mean (sd))       10.73 (1.02)           
##   stage (%)                                        
##      1                         21 ( 5.1)           
##      2                         92 (22.3)           
##      3                        155 (37.6)           
##      4                        144 (35.0)
## median [range]
print(tableOne, nonnormal = nonNormalVars, minMax = TRUE)
##                            
##                             Overall                   
##   n                         418                       
##   time (mean (sd))          1917.78 (1104.67)         
##   trt = 2 (%)                   154 (49.4)            
##   age (mean (sd))             50.74 (10.45)           
##   sex = f (%)                   374 (89.5)            
##   ascites = 1 (%)                24 (7.7)             
##   hepato = 1 (%)                160 (51.3)            
##   spiders = 1 (%)                90 (28.8)            
##   edema (%)                                           
##      0                          354 (84.7)            
##      0.5                         44 (10.5)            
##      1                           20 ( 4.8)            
##   bili (median [range])        1.40 [0.30, 28.00]     
##   chol (median [range])      309.50 [120.00, 1775.00] 
##   albumin (mean (sd))          3.50 (0.42)            
##   copper (median [range])     73.00 [4.00, 588.00]    
##   alk.phos (median [range]) 1259.00 [289.00, 13862.40]
##   ast (mean (sd))            122.56 (56.70)           
##   trig (mean (sd))           124.70 (65.15)           
##   platelet (mean (sd))       257.02 (98.33)           
##   protime (mean (sd))         10.73 (1.02)            
##   stage (%)                                           
##      1                           21 ( 5.1)            
##      2                           92 (22.3)            
##      3                          155 (37.6)            
##      4                          144 (35.0)
## without explanation
print(tableOne, nonnormal = nonNormalVars, explain = F)
##              
##               Overall                  
##   n           418                      
##   time        1917.78 (1104.67)        
##   trt = 2         154 (49.4)           
##   age           50.74 (10.45)          
##   sex = f         374 (89.5)           
##   ascites = 1      24 (7.7)            
##   hepato = 1      160 (51.3)           
##   spiders = 1      90 (28.8)           
##   edema                                
##      0            354 (84.7)           
##      0.5           44 (10.5)           
##      1             20 ( 4.8)           
##   bili           1.40 [0.80, 3.40]     
##   chol         309.50 [249.50, 400.00] 
##   albumin        3.50 (0.42)           
##   copper        73.00 [41.25, 123.00]  
##   alk.phos    1259.00 [871.50, 1980.00]
##   ast          122.56 (56.70)          
##   trig         124.70 (65.15)          
##   platelet     257.02 (98.33)          
##   protime       10.73 (1.02)           
##   stage                                
##      1             21 ( 5.1)           
##      2             92 (22.3)           
##      3            155 (37.6)           
##      4            144 (35.0)


### Stratified
## Constructor
tableOne <- CreateTableOne(vars = vars, strata = strata, data = dataFrame)
## Print method
print(tableOne)
##                       Stratified by status
##                        0                 1                2                 p      test
##   n                    232               25               161                          
##   time (mean (sd))     2333.16 (994.66)  1546.20 (753.07) 1376.93 (1049.23) <0.001     
##   trt = 2 (%)               85 (50.6)          9 (47.4)        60 (48.0)     0.894     
##   age (mean (sd))        49.51 (10.43)     41.69 (6.33)     53.92 (9.81)    <0.001     
##   sex = f (%)              215 (92.7)         22 (88.0)       137 (85.1)     0.053     
##   ascites = 1 (%)            1 (0.6)           0 (0.0)         23 (18.4)    <0.001     
##   hepato = 1 (%)            60 (35.7)         12 (63.2)        88 (70.4)    <0.001     
##   spiders = 1 (%)           33 (19.6)          5 (26.3)        52 (41.6)    <0.001     
##   edema (%)                                                                 <0.001     
##      0                     216 (93.1)         22 (88.0)       116 (72.0)               
##      0.5                    15 ( 6.5)          3 (12.0)        26 (16.1)               
##      1                       1 ( 0.4)          0 ( 0.0)        19 (11.8)               
##   bili (mean (sd))        1.58 (1.91)       3.56 (3.60)      5.54 (5.84)    <0.001     
##   chol (mean (sd))      326.54 (165.80)   439.50 (335.54)  415.75 (275.03)   0.003     
##   albumin (mean (sd))     3.59 (0.36)       3.49 (0.46)      3.36 (0.47)    <0.001     
##   copper (mean (sd))     66.61 (57.05)    124.00 (100.08)  135.41 (98.50)   <0.001     
##   alk.phos (mean (sd)) 1578.13 (1633.07) 1535.21 (837.75) 2594.35 (2677.11) <0.001     
##   ast (mean (sd))       107.29 (52.79)    130.12 (36.95)   141.93 (58.38)   <0.001     
##   trig (mean (sd))      111.79 (48.28)    133.94 (70.55)   140.49 (79.26)    0.001     
##   platelet (mean (sd))  261.16 (88.61)    309.60 (102.65)  242.49 (107.88)   0.004     
##   protime (mean (sd))    10.45 (0.92)      10.36 (0.54)     11.19 (1.05)    <0.001     
##   stage (%)                                                                 <0.001     
##      1                      19 ( 8.3)          0 ( 0.0)         2 ( 1.3)               
##      2                      64 (27.8)          5 (20.0)        23 (14.6)               
##      3                      97 (42.2)         10 (40.0)        48 (30.6)               
##      4                      50 (21.7)         10 (40.0)        84 (53.5)
## Summary method
summary(tableOne)
## 
##      ### Summary of continuous variables ###
## 
## status: 0
##            n miss mean     sd median    p25  p75   min   max skew kurt
## time     232    0 2333  994.7 2186.5 1456.5 2980 691.0  4795  0.6 -0.6
## age      232    0   50   10.4   49.8   41.4   57  26.3    78  0.1 -0.6
## bili     232    0    2    1.9    0.9    0.6    2   0.3    14  3.5 14.5
## chol     232   80  327  165.8  292.0  245.2  360 120.0  1480  4.3 24.8
## albumin  232    0    4    0.4    3.6    3.4    4   2.4     5 -0.2  0.4
## copper   232   65   67   57.1   52.0   33.5   78   4.0   412  3.1 13.5
## alk.phos 232   64 1578 1633.1 1107.5  794.0 1641 289.0 11047  3.6 14.7
## ast      232   64  107   52.8   94.5   71.3  129  26.4   457  2.4 11.2
## trig     232   81  112   48.3  104.0   80.0  134  44.0   382  1.7  5.9
## platelet 232    5  261   88.6  256.0  203.5  313  79.0   539  0.4  0.2
## protime  232    1   10    0.9   10.4    9.9   11   9.0    18  4.2 29.7
## --------------------------------------------------------------------------------------- 
## status: 1
##           n miss mean    sd median  p25  p75   min  max  skew kurt
## time     25    0 1546 753.1   1435  901 2241 533.0 3092  0.60 -0.7
## age      25    0   42   6.3     41   36   47  33.0   56  0.47 -0.6
## bili     25    0    4   3.6      3    1    4   0.5   18  2.95 10.9
## chol     25    7  440 335.5    344  312  446 201.0 1712  3.55 13.8
## albumin  25    0    3   0.5      4    3    4   2.3    4 -0.53  0.3
## copper   25    6  124 100.1    102   69  130  32.0  444  2.25  5.6
## alk.phos 25    6 1535 837.7   1345 1028 1778 637.0 3681  1.47  1.8
## ast      25    6  130  36.9    127  114  151  54.2  205  0.09  0.3
## trig     25    7  134  70.5    124   86  146  33.0  322  1.25  1.8
## platelet 25    0  310 102.7    304  248  372  76.0  517 -0.11  0.2
## protime  25    0   10   0.5     10   10   11   9.6   11  0.52 -0.7
## --------------------------------------------------------------------------------------- 
## status: 2
##            n miss mean     sd median  p25  p75   min   max  skew kurt
## time     161    0 1377 1049.2   1083  597 2071  41.0  4191  0.83 -0.2
## age      161    0   54    9.8     53   47   61  30.9    77 -0.01 -0.6
## bili     161    0    6    5.8      3    1    7   0.3    28  1.68  2.4
## chol     161   47  416  275.0    339  258  454 127.0  1775  2.58  8.1
## albumin  161    0    3    0.5      3    3    4   2.0     5 -0.33  0.2
## copper   161   37  135   98.5    111   64  199  13.0   588  1.84  5.5
## alk.phos 161   36 2594 2677.1   1664 1029 2468 516.0 13862  2.30  5.1
## ast      161   36  142   58.4    135   99  177  28.4   338  0.81  1.0
## trig     161   48  140   79.3    122   91  171  49.0   598  2.51 10.3
## platelet 161    6  242  107.9    224  158  312  62.0   721  1.01  1.8
## protime  161    1   11    1.0     11   10   12   9.0    15  0.78  0.9
## 
## p-values
##            pNormal pNonNormal
## time     2.170e-18  1.070e-18
## age      4.513e-09  7.523e-09
## bili     4.006e-19  4.088e-26
## chol     3.118e-03  3.588e-03
## albumin  4.024e-07  2.875e-06
## copper   5.999e-12  2.935e-14
## alk.phos 1.655e-04  1.139e-05
## ast      7.554e-07  2.198e-08
## trig     1.401e-03  5.535e-03
## platelet 4.024e-03  8.025e-04
## protime  6.961e-13  1.987e-15
## 
## =======================================================================================
## 
##      ### Summary of categorical variables ### 
## 
## status: 0
##      var   n miss level freq percent cum.percent
##      trt 232   64     1   83    49.4        49.4
##                       2   85    50.6       100.0
##                                                 
##      sex 232    0     m   17     7.3         7.3
##                       f  215    92.7       100.0
##                                                 
##  ascites 232   64     0  167    99.4        99.4
##                       1    1     0.6       100.0
##                                                 
##   hepato 232   64     0  108    64.3        64.3
##                       1   60    35.7       100.0
##                                                 
##  spiders 232   64     0  135    80.4        80.4
##                       1   33    19.6       100.0
##                                                 
##    edema 232    0     0  216    93.1        93.1
##                     0.5   15     6.5        99.6
##                       1    1     0.4       100.0
##                                                 
##    stage 232    2     1   19     8.3         8.3
##                       2   64    27.8        36.1
##                       3   97    42.2        78.3
##                       4   50    21.7       100.0
##                                                 
## --------------------------------------------------------------------------------------- 
## status: 1
##      var  n miss level freq percent cum.percent
##      trt 25    6     1   10    52.6        52.6
##                      2    9    47.4       100.0
##                                                
##      sex 25    0     m    3    12.0        12.0
##                      f   22    88.0       100.0
##                                                
##  ascites 25    6     0   19   100.0       100.0
##                      1    0     0.0       100.0
##                                                
##   hepato 25    6     0    7    36.8        36.8
##                      1   12    63.2       100.0
##                                                
##  spiders 25    6     0   14    73.7        73.7
##                      1    5    26.3       100.0
##                                                
##    edema 25    0     0   22    88.0        88.0
##                    0.5    3    12.0       100.0
##                      1    0     0.0       100.0
##                                                
##    stage 25    0     1    0     0.0         0.0
##                      2    5    20.0        20.0
##                      3   10    40.0        60.0
##                      4   10    40.0       100.0
##                                                
## --------------------------------------------------------------------------------------- 
## status: 2
##      var   n miss level freq percent cum.percent
##      trt 161   36     1   65    52.0        52.0
##                       2   60    48.0       100.0
##                                                 
##      sex 161    0     m   24    14.9        14.9
##                       f  137    85.1       100.0
##                                                 
##  ascites 161   36     0  102    81.6        81.6
##                       1   23    18.4       100.0
##                                                 
##   hepato 161   36     0   37    29.6        29.6
##                       1   88    70.4       100.0
##                                                 
##  spiders 161   36     0   73    58.4        58.4
##                       1   52    41.6       100.0
##                                                 
##    edema 161    0     0  116    72.0        72.0
##                     0.5   26    16.1        88.2
##                       1   19    11.8       100.0
##                                                 
##    stage 161    4     1    2     1.3         1.3
##                       2   23    14.6        15.9
##                       3   48    30.6        46.5
##                       4   84    53.5       100.0
##                                                 
## 
## p-values
##               pApprox         pExact
## trt     0.89350974940 0.884221883654
## sex     0.05344266826 0.041912591395
## ascites 0.00000004847 0.000000019153
## hepato  0.00000001808 0.000000007246
## spiders 0.00021408606 0.000194188617
## edema   0.00000003438 0.000000019547
## stage   0.00000001118             NA
## print method options
print(tableOne , exact = c("trt","stage"), format = "p", nonnormal = nonNormalVars)
##                          Stratified by status
##                           0                         1                          2                          p     
##   n                       232                       25                         161                              
##   time (mean (sd))        2333.16 (994.66)          1546.20 (753.07)           1376.93 (1049.23)          <0.001
##   trt = 2 (%)                50.6                      47.4                       48.0                     0.884
##   age (mean (sd))           49.51 (10.43)             41.69 (6.33)               53.92 (9.81)             <0.001
##   sex = f (%)                92.7                      88.0                       85.1                     0.053
##   ascites = 1 (%)             0.6                       0.0                       18.4                    <0.001
##   hepato = 1 (%)             35.7                      63.2                       70.4                    <0.001
##   spiders = 1 (%)            19.6                      26.3                       41.6                    <0.001
##   edema (%)                                                                                               <0.001
##      0                       93.1                      88.0                       72.0                          
##      0.5                      6.5                      12.0                       16.1                          
##      1                        0.4                       0.0                       11.8                          
##   bili (median [IQR])        0.90 [0.60, 1.60]         3.10 [1.30, 3.50]          3.20 [1.40, 7.10]       <0.001
##   chol (median [IQR])      292.00 [245.25, 360.00]   343.50 [311.50, 445.50]    339.00 [257.50, 454.00]    0.004
##   albumin (mean (sd))        3.59 (0.36)               3.49 (0.46)                3.36 (0.47)             <0.001
##   copper (median [IQR])     52.00 [33.50, 77.50]     102.00 [69.00, 129.50]     111.00 [63.50, 199.25]    <0.001
##   alk.phos (median [IQR]) 1107.50 [794.00, 1641.00] 1345.00 [1028.50, 1777.50] 1664.00 [1029.00, 2468.00] <0.001
##   ast (mean (sd))          107.29 (52.79)            130.12 (36.95)             141.93 (58.38)            <0.001
##   trig (mean (sd))         111.79 (48.28)            133.94 (70.55)             140.49 (79.26)             0.001
##   platelet (mean (sd))     261.16 (88.61)            309.60 (102.65)            242.49 (107.88)            0.004
##   protime (mean (sd))       10.45 (0.92)              10.36 (0.54)               11.19 (1.05)             <0.001
##   stage (%)                                                                                                NA   
##      1                        8.3                       0.0                        1.3                          
##      2                       27.8                      20.0                       14.6                          
##      3                       42.2                      40.0                       30.6                          
##      4                       21.7                      40.0                       53.5                          
##                          Stratified by status
##                           test   
##   n                              
##   time (mean (sd))               
##   trt = 2 (%)             exact  
##   age (mean (sd))                
##   sex = f (%)                    
##   ascites = 1 (%)                
##   hepato = 1 (%)                 
##   spiders = 1 (%)                
##   edema (%)                      
##      0                           
##      0.5                         
##      1                           
##   bili (median [IQR])     nonnorm
##   chol (median [IQR])     nonnorm
##   albumin (mean (sd))            
##   copper (median [IQR])   nonnorm
##   alk.phos (median [IQR]) nonnorm
##   ast (mean (sd))                
##   trig (mean (sd))               
##   platelet (mean (sd))           
##   protime (mean (sd))            
##   stage (%)               exact  
##      1                           
##      2                           
##      3                           
##      4
## median [range]
print(tableOne , exact = c("trt","stage"), format = "p", nonnormal = nonNormalVars, minMax = TRUE)
##                            Stratified by status
##                             0                          1                         2                          p     
##   n                         232                        25                        161                              
##   time (mean (sd))          2333.16 (994.66)           1546.20 (753.07)          1376.93 (1049.23)          <0.001
##   trt = 2 (%)                  50.6                       47.4                      48.0                     0.884
##   age (mean (sd))             49.51 (10.43)              41.69 (6.33)              53.92 (9.81)             <0.001
##   sex = f (%)                  92.7                       88.0                      85.1                     0.053
##   ascites = 1 (%)               0.6                        0.0                      18.4                    <0.001
##   hepato = 1 (%)               35.7                       63.2                      70.4                    <0.001
##   spiders = 1 (%)              19.6                       26.3                      41.6                    <0.001
##   edema (%)                                                                                                 <0.001
##      0                         93.1                       88.0                      72.0                          
##      0.5                        6.5                       12.0                      16.1                          
##      1                          0.4                        0.0                      11.8                          
##   bili (median [range])        0.90 [0.30, 13.60]         3.10 [0.50, 18.00]        3.20 [0.30, 28.00]      <0.001
##   chol (median [range])      292.00 [120.00, 1480.00]   343.50 [201.00, 1712.00]  339.00 [127.00, 1775.00]   0.004
##   albumin (mean (sd))          3.59 (0.36)                3.49 (0.46)               3.36 (0.47)             <0.001
##   copper (median [range])     52.00 [4.00, 412.00]      102.00 [32.00, 444.00]    111.00 [13.00, 588.00]    <0.001
##   alk.phos (median [range]) 1107.50 [289.00, 11046.60] 1345.00 [637.00, 3681.00] 1664.00 [516.00, 13862.40] <0.001
##   ast (mean (sd))            107.29 (52.79)             130.12 (36.95)            141.93 (58.38)            <0.001
##   trig (mean (sd))           111.79 (48.28)             133.94 (70.55)            140.49 (79.26)             0.001
##   platelet (mean (sd))       261.16 (88.61)             309.60 (102.65)           242.49 (107.88)            0.004
##   protime (mean (sd))         10.45 (0.92)               10.36 (0.54)              11.19 (1.05)             <0.001
##   stage (%)                                                                                                  NA   
##      1                          8.3                        0.0                       1.3                          
##      2                         27.8                       20.0                      14.6                          
##      3                         42.2                       40.0                      30.6                          
##      4                         21.7                       40.0                      53.5                          
##                            Stratified by status
##                             test   
##   n                                
##   time (mean (sd))                 
##   trt = 2 (%)               exact  
##   age (mean (sd))                  
##   sex = f (%)                      
##   ascites = 1 (%)                  
##   hepato = 1 (%)                   
##   spiders = 1 (%)                  
##   edema (%)                        
##      0                             
##      0.5                           
##      1                             
##   bili (median [range])     nonnorm
##   chol (median [range])     nonnorm
##   albumin (mean (sd))              
##   copper (median [range])   nonnorm
##   alk.phos (median [range]) nonnorm
##   ast (mean (sd))                  
##   trig (mean (sd))                 
##   platelet (mean (sd))             
##   protime (mean (sd))              
##   stage (%)                 exact  
##      1                             
##      2                             
##      3                             
##      4
## No test
print(tableOne , exact = c("trt","stage"), format = "f", nonnormal = nonNormalVars, test = FALSE)
##                          Stratified by status
##                           0                         1                          2                         
##   n                       232                       25                         161                       
##   time (mean (sd))        2333.16 (994.66)          1546.20 (753.07)           1376.93 (1049.23)         
##   trt = 2                      85                         9                         60                   
##   age (mean (sd))           49.51 (10.43)             41.69 (6.33)               53.92 (9.81)            
##   sex = f                     215                        22                        137                   
##   ascites = 1                   1                         0                         23                   
##   hepato = 1                   60                        12                         88                   
##   spiders = 1                  33                         5                         52                   
##   edema                                                                                                  
##      0                        216                        22                        116                   
##      0.5                       15                         3                         26                   
##      1                          1                         0                         19                   
##   bili (median [IQR])        0.90 [0.60, 1.60]         3.10 [1.30, 3.50]          3.20 [1.40, 7.10]      
##   chol (median [IQR])      292.00 [245.25, 360.00]   343.50 [311.50, 445.50]    339.00 [257.50, 454.00]  
##   albumin (mean (sd))        3.59 (0.36)               3.49 (0.46)                3.36 (0.47)            
##   copper (median [IQR])     52.00 [33.50, 77.50]     102.00 [69.00, 129.50]     111.00 [63.50, 199.25]   
##   alk.phos (median [IQR]) 1107.50 [794.00, 1641.00] 1345.00 [1028.50, 1777.50] 1664.00 [1029.00, 2468.00]
##   ast (mean (sd))          107.29 (52.79)            130.12 (36.95)             141.93 (58.38)           
##   trig (mean (sd))         111.79 (48.28)            133.94 (70.55)             140.49 (79.26)           
##   platelet (mean (sd))     261.16 (88.61)            309.60 (102.65)            242.49 (107.88)          
##   protime (mean (sd))       10.45 (0.92)              10.36 (0.54)               11.19 (1.05)            
##   stage                                                                                                  
##      1                         19                         0                          2                   
##      2                         64                         5                         23                   
##      3                         97                        10                         48                   
##      4                         50                        10                         84
## Quote
print(tableOne , exact = c("trt","stage"), format = "f", nonnormal = nonNormalVars, quote = TRUE)
##                            "Stratified by status"
##  ""                         "0"                         "1"                          "2"                         
##   "n"                       "232"                       "25"                         "161"                       
##   "time (mean (sd))"        "2333.16 (994.66)"          "1546.20 (753.07)"           "1376.93 (1049.23)"         
##   "trt = 2"                 "     85"                   "      9"                    "     60"                   
##   "age (mean (sd))"         "  49.51 (10.43)"           "  41.69 (6.33)"             "  53.92 (9.81)"            
##   "sex = f"                 "    215"                   "     22"                    "    137"                   
##   "ascites = 1"             "      1"                   "      0"                    "     23"                   
##   "hepato = 1"              "     60"                   "     12"                    "     88"                   
##   "spiders = 1"             "     33"                   "      5"                    "     52"                   
##   "edema"                   "       "                   "       "                    "       "                   
##   "   0"                    "    216"                   "     22"                    "    116"                   
##   "   0.5"                  "     15"                   "      3"                    "     26"                   
##   "   1"                    "      1"                   "      0"                    "     19"                   
##   "bili (median [IQR])"     "   0.90 [0.60, 1.60]"      "   3.10 [1.30, 3.50]"       "   3.20 [1.40, 7.10]"      
##   "chol (median [IQR])"     " 292.00 [245.25, 360.00]"  " 343.50 [311.50, 445.50]"   " 339.00 [257.50, 454.00]"  
##   "albumin (mean (sd))"     "   3.59 (0.36)"            "   3.49 (0.46)"             "   3.36 (0.47)"            
##   "copper (median [IQR])"   "  52.00 [33.50, 77.50]"    " 102.00 [69.00, 129.50]"    " 111.00 [63.50, 199.25]"   
##   "alk.phos (median [IQR])" "1107.50 [794.00, 1641.00]" "1345.00 [1028.50, 1777.50]" "1664.00 [1029.00, 2468.00]"
##   "ast (mean (sd))"         " 107.29 (52.79)"           " 130.12 (36.95)"            " 141.93 (58.38)"           
##   "trig (mean (sd))"        " 111.79 (48.28)"           " 133.94 (70.55)"            " 140.49 (79.26)"           
##   "platelet (mean (sd))"    " 261.16 (88.61)"           " 309.60 (102.65)"           " 242.49 (107.88)"          
##   "protime (mean (sd))"     "  10.45 (0.92)"            "  10.36 (0.54)"             "  11.19 (1.05)"            
##   "stage"                   "       "                   "       "                    "       "                   
##   "   1"                    "     19"                   "      0"                    "      2"                   
##   "   2"                    "     64"                   "      5"                    "     23"                   
##   "   3"                    "     97"                   "     10"                    "     48"                   
##   "   4"                    "     50"                   "     10"                    "     84"                   
##                            "Stratified by status"
##  ""                         "p"      "test"   
##   "n"                       ""       ""       
##   "time (mean (sd))"        "<0.001" ""       
##   "trt = 2"                 " 0.884" "exact"  
##   "age (mean (sd))"         "<0.001" ""       
##   "sex = f"                 " 0.053" ""       
##   "ascites = 1"             "<0.001" ""       
##   "hepato = 1"              "<0.001" ""       
##   "spiders = 1"             "<0.001" ""       
##   "edema"                   "<0.001" ""       
##   "   0"                    ""       ""       
##   "   0.5"                  ""       ""       
##   "   1"                    ""       ""       
##   "bili (median [IQR])"     "<0.001" "nonnorm"
##   "chol (median [IQR])"     " 0.004" "nonnorm"
##   "albumin (mean (sd))"     "<0.001" ""       
##   "copper (median [IQR])"   "<0.001" "nonnorm"
##   "alk.phos (median [IQR])" "<0.001" "nonnorm"
##   "ast (mean (sd))"         "<0.001" ""       
##   "trig (mean (sd))"        " 0.001" ""       
##   "platelet (mean (sd))"    " 0.004" ""       
##   "protime (mean (sd))"     "<0.001" ""       
##   "stage"                   " NA"    "exact"  
##   "   1"                    ""       ""       
##   "   2"                    ""       ""       
##   "   3"                    ""       ""       
##   "   4"                    ""       ""
## Digits
print(tableOne , catDigits = 2, contDigits = 5)
##                       Stratified by status
##                        0                       1                      2                       p      test
##   n                    232                     25                     161                                
##   time (mean (sd))     2333.15517 (994.65895)  1546.20000 (753.07425) 1376.93168 (1049.22797) <0.001     
##   trt = 2 (%)                  85 (50.60)               9 (47.37)             60 (48.00)       0.894     
##   age (mean (sd))        49.51034 (10.42672)     41.69342 (6.33435)     53.92071 (9.81423)    <0.001     
##   sex = f (%)                 215 (92.67)              22 (88.00)            137 (85.09)       0.053     
##   ascites = 1 (%)               1 (0.60)                0 (0.00)              23 (18.40)      <0.001     
##   hepato = 1 (%)               60 (35.71)              12 (63.16)             88 (70.40)      <0.001     
##   spiders = 1 (%)              33 (19.64)               5 (26.32)             52 (41.60)      <0.001     
##   edema (%)                                                                                   <0.001     
##      0                        216 (93.10)              22 (88.00)            116 (72.05)                 
##      0.5                       15 ( 6.47)               3 (12.00)             26 (16.15)                 
##      1                          1 ( 0.43)               0 ( 0.00)             19 (11.80)                 
##   bili (mean (sd))        1.57586 (1.91026)       3.55600 (3.59619)      5.53913 (5.83710)    <0.001     
##   chol (mean (sd))      326.53947 (165.79729)   439.50000 (335.53666)  415.75439 (275.03076)   0.003     
##   albumin (mean (sd))     3.59349 (0.36041)       3.48760 (0.45599)      3.36056 (0.46875)    <0.001     
##   copper (mean (sd))     66.61078 (57.05418)    124.00000 (100.07830)  135.41129 (98.49890)   <0.001     
##   alk.phos (mean (sd)) 1578.12738 (1633.07092) 1535.21053 (837.74808) 2594.35360 (2677.10859) <0.001     
##   ast (mean (sd))       107.28595 (52.79321)    130.11579 (36.94593)   141.93072 (58.37952)   <0.001     
##   trig (mean (sd))      111.78808 (48.27775)    133.94444 (70.54700)   140.48673 (79.25818)    0.001     
##   platelet (mean (sd))  261.15859 (88.60787)    309.60000 (102.65314)  242.49032 (107.87615)   0.004     
##   protime (mean (sd))    10.45411 (0.92458)      10.36000 (0.53697)     11.19063 (1.04904)    <0.001     
##   stage (%)                                                                                   <0.001     
##      1                         19 ( 8.26)               0 ( 0.00)              2 ( 1.27)                 
##      2                         64 (27.83)               5 (20.00)             23 (14.65)                 
##      3                         97 (42.17)              10 (40.00)             48 (30.57)                 
##      4                         50 (21.74)              10 (40.00)             84 (53.50)

## Categorical only
print(tableOne$CatTable)
##                  Stratified by status
##                   0           1          2           p      test
##   n               232         25         161                    
##   trt = 2 (%)      85 (50.6)   9 (47.4)   60 (48.0)   0.894     
##   sex = f (%)     215 (92.7)  22 (88.0)  137 (85.1)   0.053     
##   ascites = 1 (%)   1 ( 0.6)   0 ( 0.0)   23 (18.4)  <0.001     
##   hepato = 1 (%)   60 (35.7)  12 (63.2)   88 (70.4)  <0.001     
##   spiders = 1 (%)  33 (19.6)   5 (26.3)   52 (41.6)  <0.001     
##   edema (%)                                          <0.001     
##      0            216 (93.1)  22 (88.0)  116 (72.0)             
##      0.5           15 ( 6.5)   3 (12.0)   26 (16.1)             
##      1              1 ( 0.4)   0 ( 0.0)   19 (11.8)             
##   stage (%)                                          <0.001     
##      1             19 ( 8.3)   0 ( 0.0)    2 ( 1.3)             
##      2             64 (27.8)   5 (20.0)   23 (14.6)             
##      3             97 (42.2)  10 (40.0)   48 (30.6)             
##      4             50 (21.7)  10 (40.0)   84 (53.5)
## Continuous only
print(tableOne$ContTable)
##                       Stratified by status
##                        0                 1                2                 p      test
##   n                    232               25               161                          
##   time (mean (sd))     2333.16 (994.66)  1546.20 (753.07) 1376.93 (1049.23) <0.001     
##   age (mean (sd))        49.51 (10.43)     41.69 (6.33)     53.92 (9.81)    <0.001     
##   bili (mean (sd))        1.58 (1.91)       3.56 (3.60)      5.54 (5.84)    <0.001     
##   chol (mean (sd))      326.54 (165.80)   439.50 (335.54)  415.75 (275.03)   0.003     
##   albumin (mean (sd))     3.59 (0.36)       3.49 (0.46)      3.36 (0.47)    <0.001     
##   copper (mean (sd))     66.61 (57.05)    124.00 (100.08)  135.41 (98.50)   <0.001     
##   alk.phos (mean (sd)) 1578.13 (1633.07) 1535.21 (837.75) 2594.35 (2677.11) <0.001     
##   ast (mean (sd))       107.29 (52.79)    130.12 (36.95)   141.93 (58.38)   <0.001     
##   trig (mean (sd))      111.79 (48.28)    133.94 (70.55)   140.49 (79.26)    0.001     
##   platelet (mean (sd))  261.16 (88.61)    309.60 (102.65)  242.49 (107.88)   0.004     
##   protime (mean (sd))    10.45 (0.92)      10.36 (0.54)     11.19 (1.05)    <0.001

### Two strata variables
## Constructor
tableOne <- CreateTableOne(vars = vars, strata = strataTwoVars, data = dataFrame)
## Print method
print(tableOne)
##                       Stratified by status:sex
##                        0:m               1:m              2:m               0:f               1:f             
##   n                    17                3                24                215               22              
##   time (mean (sd))     2462.18 (1217.33) 1147.67 (785.78) 1584.88 (1120.46) 2322.95 (977.61)  1600.55 (750.70)
##   trt = 2 (%)                7 (63.6)          0 (0.0)          8 (36.4)         78 (49.7)          9 (56.2)  
##   age (mean (sd))        55.69 (10.65)     42.16 (11.97)    57.42 (10.34)     49.02 (10.28)     41.63 (5.67)  
##   sex = f (%)                0 (0.0)           0 (0.0)          0 (0.0)         215 (100.0)        22 (100.0) 
##   ascites = 1 (%)            1 (9.1)           0 (0.0)          2 (9.1)           0 (0.0)           0 (0.0)   
##   hepato = 1 (%)             7 (63.6)          1 (33.3)        13 (59.1)         53 (33.8)         11 (68.8)  
##   spiders = 1 (%)            2 (18.2)          0 (0.0)          2 (9.1)          31 (19.7)          5 (31.2)  
##   edema (%)                                                                                                   
##      0                      16 (94.1)          3 (100.0)       17 (70.8)        200 (93.0)         19 (86.4)  
##      0.5                     1 ( 5.9)          0 (  0.0)        4 (16.7)         14 ( 6.5)          3 (13.6)  
##      1                       0 ( 0.0)          0 (  0.0)        3 (12.5)          1 ( 0.5)          0 ( 0.0)  
##   bili (mean (sd))        2.63 (2.62)       2.37 (1.15)      3.10 (2.25)       1.49 (1.83)       3.72 (3.80)  
##   chol (mean (sd))      367.91 (234.99)   415.33 (201.32)  352.05 (149.18)   323.31 (159.87)   444.33 (361.62)
##   albumin (mean (sd))     3.47 (0.43)       3.75 (0.28)      3.55 (0.50)       3.60 (0.35)       3.45 (0.47)  
##   copper (mean (sd))    128.82 (113.07)   215.33 (198.03)  158.68 (78.57)     62.22 (48.66)    106.88 (69.34) 
##   alk.phos (mean (sd)) 2482.25 (3052.94)  921.00 (196.50) 2189.02 (2243.18) 1514.78 (1481.83) 1650.38 (864.55)
##   ast (mean (sd))       112.28 (50.94)    110.73 (30.99)   128.38 (47.39)    106.94 (53.06)    133.75 (37.69) 
##   trig (mean (sd))      131.82 (33.73)    165.00 (64.58)   129.76 (58.95)    110.21 (48.98)    127.73 (72.11) 
##   platelet (mean (sd))  213.44 (72.17)    289.00 (65.18)   235.71 (93.92)    264.78 (88.83)    312.41 (107.56)
##   protime (mean (sd))    10.94 (0.80)      10.73 (0.51)     10.97 (1.07)      10.42 (0.92)      10.31 (0.53)  
##   stage (%)                                                                                                   
##      1                       2 (11.8)          0 ( 0.0)         1 ( 4.2)         17 ( 8.0)          0 ( 0.0)  
##      2                       3 (17.6)          0 ( 0.0)         5 (20.8)         61 (28.6)          5 (22.7)  
##      3                       5 (29.4)          2 (66.7)         9 (37.5)         92 (43.2)          8 (36.4)  
##      4                       7 (41.2)          1 (33.3)         9 (37.5)         43 (20.2)          9 (40.9)  
##                       Stratified by status:sex
##                        2:f               p      test
##   n                    137                          
##   time (mean (sd))     1340.50 (1036.28) <0.001     
##   trt = 2 (%)               52 (50.5)     0.340     
##   age (mean (sd))        53.31 (9.63)    <0.001     
##   sex = f (%)              137 (100.0)   <0.001     
##   ascites = 1 (%)           21 (20.4)    <0.001     
##   hepato = 1 (%)            75 (72.8)    <0.001     
##   spiders = 1 (%)           50 (48.5)    <0.001     
##   edema (%)                              <0.001     
##      0                      99 (72.3)               
##      0.5                    22 (16.1)               
##      1                      16 (11.7)               
##   bili (mean (sd))        5.97 (6.16)    <0.001     
##   chol (mean (sd))      430.14 (294.85)   0.016     
##   albumin (mean (sd))     3.33 (0.46)    <0.001     
##   copper (mean (sd))    130.39 (101.92)  <0.001     
##   alk.phos (mean (sd)) 2680.93 (2762.92)  0.001     
##   ast (mean (sd))       144.82 (60.27)   <0.001     
##   trig (mean (sd))      142.93 (83.28)    0.007     
##   platelet (mean (sd))  243.73 (110.52)   0.009     
##   protime (mean (sd))    11.23 (1.04)    <0.001     
##   stage (%)                              <0.001     
##      1                       1 ( 0.8)               
##      2                      18 (13.5)               
##      3                      39 (29.3)               
##      4                      75 (56.4)
## Summary method
print(tableOne)
##                       Stratified by status:sex
##                        0:m               1:m              2:m               0:f               1:f             
##   n                    17                3                24                215               22              
##   time (mean (sd))     2462.18 (1217.33) 1147.67 (785.78) 1584.88 (1120.46) 2322.95 (977.61)  1600.55 (750.70)
##   trt = 2 (%)                7 (63.6)          0 (0.0)          8 (36.4)         78 (49.7)          9 (56.2)  
##   age (mean (sd))        55.69 (10.65)     42.16 (11.97)    57.42 (10.34)     49.02 (10.28)     41.63 (5.67)  
##   sex = f (%)                0 (0.0)           0 (0.0)          0 (0.0)         215 (100.0)        22 (100.0) 
##   ascites = 1 (%)            1 (9.1)           0 (0.0)          2 (9.1)           0 (0.0)           0 (0.0)   
##   hepato = 1 (%)             7 (63.6)          1 (33.3)        13 (59.1)         53 (33.8)         11 (68.8)  
##   spiders = 1 (%)            2 (18.2)          0 (0.0)          2 (9.1)          31 (19.7)          5 (31.2)  
##   edema (%)                                                                                                   
##      0                      16 (94.1)          3 (100.0)       17 (70.8)        200 (93.0)         19 (86.4)  
##      0.5                     1 ( 5.9)          0 (  0.0)        4 (16.7)         14 ( 6.5)          3 (13.6)  
##      1                       0 ( 0.0)          0 (  0.0)        3 (12.5)          1 ( 0.5)          0 ( 0.0)  
##   bili (mean (sd))        2.63 (2.62)       2.37 (1.15)      3.10 (2.25)       1.49 (1.83)       3.72 (3.80)  
##   chol (mean (sd))      367.91 (234.99)   415.33 (201.32)  352.05 (149.18)   323.31 (159.87)   444.33 (361.62)
##   albumin (mean (sd))     3.47 (0.43)       3.75 (0.28)      3.55 (0.50)       3.60 (0.35)       3.45 (0.47)  
##   copper (mean (sd))    128.82 (113.07)   215.33 (198.03)  158.68 (78.57)     62.22 (48.66)    106.88 (69.34) 
##   alk.phos (mean (sd)) 2482.25 (3052.94)  921.00 (196.50) 2189.02 (2243.18) 1514.78 (1481.83) 1650.38 (864.55)
##   ast (mean (sd))       112.28 (50.94)    110.73 (30.99)   128.38 (47.39)    106.94 (53.06)    133.75 (37.69) 
##   trig (mean (sd))      131.82 (33.73)    165.00 (64.58)   129.76 (58.95)    110.21 (48.98)    127.73 (72.11) 
##   platelet (mean (sd))  213.44 (72.17)    289.00 (65.18)   235.71 (93.92)    264.78 (88.83)    312.41 (107.56)
##   protime (mean (sd))    10.94 (0.80)      10.73 (0.51)     10.97 (1.07)      10.42 (0.92)      10.31 (0.53)  
##   stage (%)                                                                                                   
##      1                       2 (11.8)          0 ( 0.0)         1 ( 4.2)         17 ( 8.0)          0 ( 0.0)  
##      2                       3 (17.6)          0 ( 0.0)         5 (20.8)         61 (28.6)          5 (22.7)  
##      3                       5 (29.4)          2 (66.7)         9 (37.5)         92 (43.2)          8 (36.4)  
##      4                       7 (41.2)          1 (33.3)         9 (37.5)         43 (20.2)          9 (40.9)  
##                       Stratified by status:sex
##                        2:f               p      test
##   n                    137                          
##   time (mean (sd))     1340.50 (1036.28) <0.001     
##   trt = 2 (%)               52 (50.5)     0.340     
##   age (mean (sd))        53.31 (9.63)    <0.001     
##   sex = f (%)              137 (100.0)   <0.001     
##   ascites = 1 (%)           21 (20.4)    <0.001     
##   hepato = 1 (%)            75 (72.8)    <0.001     
##   spiders = 1 (%)           50 (48.5)    <0.001     
##   edema (%)                              <0.001     
##      0                      99 (72.3)               
##      0.5                    22 (16.1)               
##      1                      16 (11.7)               
##   bili (mean (sd))        5.97 (6.16)    <0.001     
##   chol (mean (sd))      430.14 (294.85)   0.016     
##   albumin (mean (sd))     3.33 (0.46)    <0.001     
##   copper (mean (sd))    130.39 (101.92)  <0.001     
##   alk.phos (mean (sd)) 2680.93 (2762.92)  0.001     
##   ast (mean (sd))       144.82 (60.27)   <0.001     
##   trig (mean (sd))      142.93 (83.28)    0.007     
##   platelet (mean (sd))  243.73 (110.52)   0.009     
##   protime (mean (sd))    11.23 (1.04)    <0.001     
##   stage (%)                              <0.001     
##      1                       1 ( 0.8)               
##      2                      18 (13.5)               
##      3                      39 (29.3)               
##      4                      75 (56.4)

Functions

CreateTableOne
## function(vars,                                      # character vector of variable names
##              strata,                                    # character vector of variable names
##              data,                                      # data frame
##              factorVars,                                # variables to be transformed to factors
##              test          = TRUE,                      # whether to put p-values
##              ## Test configuration for categorical data
##              testApprox    = chisq.test,                # function for approximation test
##              argsApprox    = list(correct = TRUE),      # arguments passed to testApprox
##              testExact     = fisher.test,               # function for exact test
##              argsExact     = list(workspace = 2*10^5),  # arguments passed to testExact
##              ## Test configuration for continuous data
##              testNormal    = oneway.test,               # test for normally distributed variables
##              argsNormal    = list(var.equal = TRUE),    # arguments passed to testNormal
##              testNonNormal = kruskal.test,              # test for nonnormally distributed variables
##              argsNonNormal = list(NULL)                 # arguments passed to testNonNormal
##              ) {
## 
## ### Data check
##         ## Check if the data given is a dataframe
##         ModuleStopIfNotDataFrame(data)
## 
##         ## Check if variables exist. Drop them if not.
##         vars <- ModuleReturnVarsExist(vars, data)
## 
##         ## Abort if no variables exist at this point
##         ModuleStopIfNoVarsLeft(vars)
## 
##         ## Factor conversions if the factorVars argument exist
##         if (!missing(factorVars)) {
##             ## Check if variables exist. Drop them if not.
##             factorVars <- ModuleReturnVarsExist(factorVars, data)
##             ## Convert to factor
##             data[factorVars] <- lapply(data[factorVars], factor)
##         }
## 
##         ## Toggle test FALSE if no strata is given
##         test <- ModuleReturnFalseIfNoStrata(strata, test)
## 
##         ## Get the classes of the variables
##         varClasses  <- sapply(data[vars], class)
##         varFactors  <- names(varClasses[varClasses == "factor"])
##         varNumerics <- names(varClasses[varClasses == "numeric" | varClasses == "integer"])
## 
##         ## Create a logical vector indicator for factors
##         logiFactors <- sapply(data[vars], is.factor)
## 
##         ## Create lists of arguments
##         argsCreateContTable <- list(data          = data,
##                                     test          = test,
##                                     testNormal    = testNormal,
##                                     argsNormal    = argsNormal,
##                                     testNonNormal = testNonNormal,
##                                     argsNonNormal = argsNonNormal
##                                     )
##         argsCreateCatTable  <- list(data          = data,
##                                     test          = test,
##                                     testApprox    = testApprox,
##                                     argsApprox    = argsApprox,
##                                     testExact     = testExact,
##                                     argsExact     = argsExact
##                                     )
##         ## Add strata = strata for argument only if strata is given
##         if(!missing(strata)) {
## 
##             argsCreateContTable <- c(list(strata = strata), argsCreateContTable)
##             argsCreateCatTable  <- c(list(strata = strata), argsCreateCatTable)
##         }
## 
## 
##         ## Condition on the absence of factor/numeric
##         if (length(varNumerics) == 0) {
##             ## No numerics
##             cat('NOTE: no numeric/integer variables supplied, using CreateCatTable()\n')
##             CatTable <- do.call(CreateCatTable,
##                                 args = c(list(vars = varFactors), argsCreateCatTable))
##             return(CatTable)
## 
##         } else if (length(varFactors) == 0) {
##             ## No factors
##             cat('NOTE: no factor variables supplied, using CreateContTable()\n')
##             ContTable <- do.call(CreateContTable,
##                                  args = c(list(vars = varNumerics), argsCreateContTable))
##             return(ContTable)
## 
## ### Proceed if both types of variables are present (both factors and numerics)
##         } else if ((length(varFactors) > 0) & (length(varNumerics) > 0)) {
## 
##             ## Create a list of constructors
##             listOfConstructors <- list(CreateContTable = CreateContTable,
##                                        CreateCatTable  = CreateCatTable)
##             ## CreateCatTable for categorical. CreateContTable for continuous.
##             listOfConstructors <- listOfConstructors[logiFactors + 1]
##             ## Create a list of arguments
##             listOfArgs <- list(argsCreateContTable = argsCreateContTable,
##                                argsCreateCatTable  = argsCreateCatTable)
##             ## argsCreateCatTable for categorical. argsCreateContTable for continuous.
##             listOfArgs <- listOfArgs[logiFactors + 1]
## 
##             ## Create a list of tables
##             TableOne <- sapply(seq_along(listOfConstructors),
##                                FUN = function(i) {
## 
##                                    args <- c(list(vars = vars[i]),  # vector element
##                                              listOfArgs[[i]])       # list element
## 
##                                    do.call(listOfConstructors[[i]], # list element
##                                            args = args)
##                                },
##                                simplify = FALSE)
## 
##             ## Give variable names
##             names(TableOne) <- vars
## 
## 
##             ## Create ContTable and CatTable objects (this is redundant)
##             ## Aggregated ContTable
##             ContTable <- do.call(CreateContTable,
##                                  args = c(list(vars = varNumerics), argsCreateContTable))
##             ## Aggregated CatTable
##             CatTable  <- do.call(CreateCatTable,
##                                  args = c(list(vars = varFactors),  argsCreateCatTable))
## 
##             ## Create a list
##             listOfTables <- list(TableOne  = TableOne,
##                                  ContTable = ContTable,
##                                  CatTable  = CatTable
##                                  )
## 
##             ## Give a class
##             class(listOfTables) <- "TableOne"
## 
##             ## Return the object
##             return(listOfTables)
##         }
##     }
## <environment: namespace:tableone>
summary.TableOne
## function(object, digits = 1, ...) {
## 
##     ## object and ... required to be consistent with generic summary(object, ...)
##     listCatContTables <- object
## 
##     ## Continuous
##     cat("\n     ### Summary of continuous variables ###\n\n")    
##     summary(listCatContTables$ContTable, digits = digits)    
## 
##     ## Separator
##     cat("\n=======================================================================================\n")
##     
##     ## Categorical    
##     cat("\n     ### Summary of categorical variables ### \n\n")
##     summary(listCatContTables$CatTable, digits = digits)
## }
## <environment: namespace:tableone>
print.TableOne
## function(x, missing = FALSE,
##                            quote = FALSE,
##                            test = TRUE, catDigits = 1, contDigits = 2, pDigits = 3,
## 
##                            ## Categorical options
##                            format = c("fp","f","p","pf")[1], # Format f_requency and/or p_ercent
##                            exact = NULL,
##                            cramVars = NULL,
## 
##                            ## Continuous options
##                            nonnormal = NULL,
##                            minMax = FALSE,
## 
##                            ## Common options
##                            explain = TRUE,
##                            printToggle = TRUE,
##                            ...) {
## 
##     ## Get the mixed element only
##     TableOne <- x$TableOne
## 
##     ## Get the Cont/Cat status (1st of classes)
##     classOfTables <- sapply(TableOne, class)[1,]
##     digits <- c(CatTable = catDigits, ContTable = contDigits)[classOfTables]
## 
## 
##     ## Get the formatted tables
##     formattedTables <- sapply(seq_along(TableOne),
##                               FUN = function(i) {
##                                   
##                                   print(TableOne[[i]], printToggle = FALSE, test = test, explain = explain,
##                                         digits = digits[i],
##                                         ## print.CatTable arguments
##                                         format = format, exact = exact,
##                                         showAllLevels = FALSE,  # must be FALSE to get same column counts
##                                         cramVars = cramVars,
##                                         ## print.ContTable argument
##                                         nonnormal = nonnormal, minMax = minMax
##                                         )  # Method dispatch at work
##                               },
##                               simplify = FALSE)
## 
##     ## Get the column width information (strata x vars format)
##     columnWidthInfo <- sapply(formattedTables,
##                               FUN = function(matObj) {
##                                   
##                                   attributes(matObj)$vecColWidths
##                               },
##                               simplify = FALSE)
##     columnWidthInfo <- do.call(cbind, columnWidthInfo)
## 
##     ## Get the max values for each stratum
##     vecMaxValues <- apply(columnWidthInfo, MARGIN = 1, FUN = max, na.rm = TRUE)
## 
##     ## Get the difference (value - max. Must be negated)
##     nSpacesToAdd <- sweep(x      = columnWidthInfo,
##                           MARGIN = 1,
##                           STATS  = vecMaxValues,
##                           FUN    = "-"
##                           )
##     nSpacesToAdd <- -1 * nSpacesToAdd
## 
## 
##     ## For each matrix, add spaces
##     spaceFormattedTables <- sapply(seq_along(formattedTables),
##                                    FUN = function(i) {
## 
##                                        ## For i-th variable
##                                        matObj <- formattedTables[[i]]
##                                        nSpaces <- nSpacesToAdd[, i]
## 
##                                        ## For j-th stratum (column). Be aware of the p-value column
##                                        for (j in seq_along(nSpaces)) {
## 
##                                            matObj[, j] <- paste0(paste0(rep(" ", nSpaces[j]), collapse = ""),
##                                                                  matObj[, j])
##                                        }
## 
##                                        ## Return the adjusted table
##                                        matObj
##                                    },
##                                    simplify = FALSE)
## 
##     ## Set aside the n row (stratum sizes). 1st element, 1st row
##     stratumSizesRow <- spaceFormattedTables[[1]][1, , drop = FALSE]
## 
##     ## Remove 1st rows from each table (stratum sizes)
##     spaceFormattedTables <- sapply(spaceFormattedTables,
##                                    FUN = function(matObj) {
##                                        
##                                        matObj[-1, , drop = FALSE]
##                                    },
##                                    simplify = FALSE)
## 
##     ## Row-combin n and all variables
##     out <- do.call(rbind, c(list(stratumSizesRow), spaceFormattedTables))
## 
##     ## Add stratification information to the column header (This is also in the constructor)
##     if (length(TableOne[[1]]) > 1 ) {
##         ## Combine variable names with : in between
##         strataVarName <- attributes(TableOne[[1]])$strataVarName
## 
##         ## Create strata string
##         strataString <- paste0("Stratified by ", strataVarName)
## 
##         ## Name the row dimension with it. 1st dimension name should be empty.
##         names(dimnames(out)) <- c("", strataString)
##     } else {
## 
##         names(dimnames(out)) <- c("", "")
##     }
## 
##     ## Modular version of quote/print toggle.
##     out <- ModuleQuoteAndPrintMat(matObj = out,
##                                   quote = quote, printToggle = printToggle)
## 
##     ## Return the result
##     return(invisible(out))
## }
## <environment: namespace:tableone>