It is now available on CRAN. You can install it just like other packages.
install.packages("tableone")
After installation, tableone is just another R package. You can load it as follows.
## Load
library(tableone)
I will use the clinically-oriented pbc dataset included in the survival package, which you should have by default.
## Load survival
library(survival)
## Load Mayo Clinic Primary Biliary Cirrhosis (PBC) Data
data(pbc)
## Check variables
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
Variables need to be specified as a character vector.
## List numerically coded categorical variables
factorVars <- c("status","trt","ascites","hepato","spiders","edema","stage")
## Create a variable list. Use dput(names(pbc))
vars <- c("time","status","age","sex","ascites","hepato",
"spiders","edema","bili","chol","albumin",
"copper","alk.phos","ast","trig","platelet",
"protime","stage")
Then the TableOne object can be created, and examined as follows.
## Create Table 1 stratified by trt (omit strata argument for overall table)
tableOne <- CreateTableOne(vars = vars, strata = "trt", data = pbc, factorVars = factorVars)
## Just typing the object name will invoke the print.TableOne method
## Tests are by oneway.test/t.test for continuous, chisq.test for categorical
tableOne
Stratified by trt
1 2 p test
n 158 154
time (mean (sd)) 2015.62 (1094.12) 1996.86 (1155.93) 0.883
status (%) 0.894
0 83 (52.5) 85 (55.2)
1 10 ( 6.3) 9 ( 5.8)
2 65 (41.1) 60 (39.0)
age (mean (sd)) 51.42 (11.01) 48.58 (9.96) 0.018
sex = f (%) 137 (86.7) 139 (90.3) 0.421
ascites = 1 (%) 14 (8.9) 10 (6.5) 0.567
hepato = 1 (%) 73 (46.2) 87 (56.5) 0.088
spiders = 1 (%) 45 (28.5) 45 (29.2) 0.985
edema (%) 0.877
0 132 (83.5) 131 (85.1)
0.5 16 (10.1) 13 ( 8.4)
1 10 ( 6.3) 10 ( 6.5)
bili (mean (sd)) 2.87 (3.63) 3.65 (5.28) 0.131
chol (mean (sd)) 365.01 (209.54) 373.88 (252.48) 0.748
albumin (mean (sd)) 3.52 (0.44) 3.52 (0.40) 0.874
copper (mean (sd)) 97.64 (90.59) 97.65 (80.49) 0.999
alk.phos (mean (sd)) 2021.30 (2183.44) 1943.01 (2101.69) 0.747
ast (mean (sd)) 120.21 (54.52) 124.97 (58.93) 0.460
trig (mean (sd)) 124.14 (71.54) 125.25 (58.52) 0.886
platelet (mean (sd)) 258.75 (100.32) 265.20 (90.73) 0.555
protime (mean (sd)) 10.65 (0.85) 10.80 (1.14) 0.197
stage (%) 0.201
1 12 ( 7.6) 4 ( 2.6)
2 35 (22.2) 32 (20.8)
3 56 (35.4) 64 (41.6)
4 55 (34.8) 54 (35.1)
## See the categorical part (CatTable) only using $ operator
tableOne$CatTable
Stratified by trt
1 2 p test
n 158 154
status (%) 0.894
0 83 (52.5) 85 (55.2)
1 10 ( 6.3) 9 ( 5.8)
2 65 (41.1) 60 (39.0)
sex = f (%) 137 (86.7) 139 (90.3) 0.421
ascites = 1 (%) 14 ( 8.9) 10 ( 6.5) 0.567
hepato = 1 (%) 73 (46.2) 87 (56.5) 0.088
spiders = 1 (%) 45 (28.5) 45 (29.2) 0.985
edema (%) 0.877
0 132 (83.5) 131 (85.1)
0.5 16 (10.1) 13 ( 8.4)
1 10 ( 6.3) 10 ( 6.5)
stage (%) 0.201
1 12 ( 7.6) 4 ( 2.6)
2 35 (22.2) 32 (20.8)
3 56 (35.4) 64 (41.6)
4 55 (34.8) 54 (35.1)
## See the continuous part (ContTable) only using $ operator
tableOne$ContTable
Stratified by trt
1 2 p test
n 158 154
time (mean (sd)) 2015.62 (1094.12) 1996.86 (1155.93) 0.883
age (mean (sd)) 51.42 (11.01) 48.58 (9.96) 0.018
bili (mean (sd)) 2.87 (3.63) 3.65 (5.28) 0.131
chol (mean (sd)) 365.01 (209.54) 373.88 (252.48) 0.748
albumin (mean (sd)) 3.52 (0.44) 3.52 (0.40) 0.874
copper (mean (sd)) 97.64 (90.59) 97.65 (80.49) 0.999
alk.phos (mean (sd)) 2021.30 (2183.44) 1943.01 (2101.69) 0.747
ast (mean (sd)) 120.21 (54.52) 124.97 (58.93) 0.460
trig (mean (sd)) 124.14 (71.54) 125.25 (58.52) 0.886
platelet (mean (sd)) 258.75 (100.32) 265.20 (90.73) 0.555
protime (mean (sd)) 10.65 (0.85) 10.80 (1.14) 0.197
## More detailed view of categorical variables can be obtained via summary.CatTable methods
summary(tableOne$CatTable)
trt: 1
var n miss p.miss level freq percent cum.percent
status 158 0 0.0 0 83 52.5 52.5
1 10 6.3 58.9
2 65 41.1 100.0
sex 158 0 0.0 m 21 13.3 13.3
f 137 86.7 100.0
ascites 158 0 0.0 0 144 91.1 91.1
1 14 8.9 100.0
hepato 158 0 0.0 0 85 53.8 53.8
1 73 46.2 100.0
spiders 158 0 0.0 0 113 71.5 71.5
1 45 28.5 100.0
edema 158 0 0.0 0 132 83.5 83.5
0.5 16 10.1 93.7
1 10 6.3 100.0
stage 158 0 0.0 1 12 7.6 7.6
2 35 22.2 29.7
3 56 35.4 65.2
4 55 34.8 100.0
---------------------------------------------------------------------------------------
trt: 2
var n miss p.miss level freq percent cum.percent
status 154 0 0.0 0 85 55.2 55.2
1 9 5.8 61.0
2 60 39.0 100.0
sex 154 0 0.0 m 15 9.7 9.7
f 139 90.3 100.0
ascites 154 0 0.0 0 144 93.5 93.5
1 10 6.5 100.0
hepato 154 0 0.0 0 67 43.5 43.5
1 87 56.5 100.0
spiders 154 0 0.0 0 109 70.8 70.8
1 45 29.2 100.0
edema 154 0 0.0 0 131 85.1 85.1
0.5 13 8.4 93.5
1 10 6.5 100.0
stage 154 0 0.0 1 4 2.6 2.6
2 32 20.8 23.4
3 64 41.6 64.9
4 54 35.1 100.0
p-values
pApprox pExact
status 0.89351 0.88422
sex 0.42123 0.37743
ascites 0.56729 0.52558
hepato 0.08821 0.07138
spiders 0.98466 0.90114
edema 0.87682 0.89370
stage 0.20130 0.20456
## More detailed view of continuous variables can be obtained via summary.ContTable methods
summary(tableOne$ContTable)
trt: 1
n miss p.miss mean sd median p25 p75 min max skew kurt
time 158 0 0.00 2015.6 1094.12 1895.0 1231.0 2632.5 41.0 4556.0 0.412 -0.44
age 158 0 0.00 51.4 11.01 51.9 43.0 58.9 26.3 78.4 0.056 -0.53
bili 158 0 0.00 2.9 3.63 1.4 0.8 3.2 0.3 20.0 2.675 7.61
chol 158 18 11.39 365.0 209.54 315.5 247.8 417.0 127.0 1712.0 3.831 20.21
albumin 158 0 0.00 3.5 0.44 3.6 3.2 3.8 2.1 4.6 -0.395 0.30
copper 158 1 0.63 97.6 90.59 73.0 40.0 121.0 9.0 588.0 2.503 8.21
alk.phos 158 0 0.00 2021.3 2183.44 1214.5 840.8 2028.0 369.0 11552.0 2.707 7.36
ast 158 0 0.00 120.2 54.52 111.6 76.7 151.5 26.4 338.0 1.095 1.60
trig 158 19 12.03 124.1 71.54 106.0 84.5 146.0 33.0 598.0 2.947 14.29
platelet 158 2 1.27 258.8 100.32 255.0 189.5 322.0 62.0 563.0 0.498 0.25
protime 158 0 0.00 10.7 0.85 10.6 10.0 11.0 9.0 14.1 1.103 1.62
---------------------------------------------------------------------------------------
trt: 2
n miss p.miss mean sd median p25 p75 min max skew kurt
time 154 0 0.00 1996.9 1155.9 1811.0 1153.00 2771.2 51.0 4523.0 0.35 -0.72
age 154 0 0.00 48.6 10.0 48.1 41.43 55.8 30.6 74.5 0.24 -0.53
bili 154 0 0.00 3.6 5.3 1.3 0.72 3.6 0.3 28.0 2.67 7.27
chol 154 10 6.49 373.9 252.5 303.5 254.25 377.0 120.0 1775.0 3.12 11.14
albumin 154 0 0.00 3.5 0.4 3.5 3.34 3.8 2.0 4.4 -0.85 1.96
copper 154 1 0.65 97.7 80.5 73.0 43.00 139.0 4.0 558.0 2.01 6.65
alk.phos 154 0 0.00 1943.0 2101.7 1283.0 922.50 1949.8 289.0 13862.4 3.35 12.81
ast 154 0 0.00 125.0 58.9 117.4 83.78 151.9 28.4 457.2 1.73 6.31
trig 154 11 7.14 125.3 58.5 113.0 84.50 155.0 44.0 432.0 1.72 5.51
platelet 154 2 1.30 265.2 90.7 259.5 206.75 322.5 71.0 487.0 0.23 -0.35
protime 154 0 0.00 10.8 1.1 10.6 10.00 11.4 9.2 17.1 1.87 6.39
p-values
pNormal pNonNormal
time 0.88305 0.82662
age 0.01767 0.01962
bili 0.13094 0.84168
chol 0.74799 0.54434
albumin 0.87388 0.95045
copper 0.99916 0.71745
alk.phos 0.74726 0.81198
ast 0.45970 0.45892
trig 0.88604 0.36980
platelet 0.55451 0.45483
protime 0.19714 0.58802
The bili, chol, copper, alk.phos, and trig continuous variables are highly right skewed, as typical in untransformed biomarkers. They should be treated as nonnormal variables. Some cells are a bit small for the status and stage categorical variables, so it may be better to use exact tests.
## Specifying nonnormal variables will show the variables appropriately,
## and show nonparametric test p-values (kruskal.test/wilcox.test).
## Specify variables in the exact argument to obtain the exact test
## (fisher.test) p-values. If a 2-level factor is specified in cramVars
## both levels are shown in one row.
print(tableOne, nonnormal = c("bili","chol","copper","alk.phos","trig"),
exact = c("status","stage"), cramVars = "sex")
Stratified by trt
1 2 p test
n 158 154
time (mean (sd)) 2015.62 (1094.12) 1996.86 (1155.93) 0.883
status (%) 0.884 exact
0 83 (52.5) 85 (55.2)
1 10 ( 6.3) 9 ( 5.8)
2 65 (41.1) 60 (39.0)
age (mean (sd)) 51.42 (11.01) 48.58 (9.96) 0.018
sex = m/f (%) 21/137 (13.3/86.7) 15/139 (9.7/90.3) 0.421
ascites = 1 (%) 14 (8.9) 10 (6.5) 0.567
hepato = 1 (%) 73 (46.2) 87 (56.5) 0.088
spiders = 1 (%) 45 (28.5) 45 (29.2) 0.985
edema (%) 0.877
0 132 (83.5) 131 (85.1)
0.5 16 (10.1) 13 ( 8.4)
1 10 ( 6.3) 10 ( 6.5)
bili (median [IQR]) 1.40 [0.80, 3.20] 1.30 [0.72, 3.60] 0.842 nonnorm
chol (median [IQR]) 315.50 [247.75, 417.00] 303.50 [254.25, 377.00] 0.544 nonnorm
albumin (mean (sd)) 3.52 (0.44) 3.52 (0.40) 0.874
copper (median [IQR]) 73.00 [40.00, 121.00] 73.00 [43.00, 139.00] 0.717 nonnorm
alk.phos (median [IQR]) 1214.50 [840.75, 2028.00] 1283.00 [922.50, 1949.75] 0.812 nonnorm
ast (mean (sd)) 120.21 (54.52) 124.97 (58.93) 0.460
trig (median [IQR]) 106.00 [84.50, 146.00] 113.00 [84.50, 155.00] 0.370 nonnorm
platelet (mean (sd)) 258.75 (100.32) 265.20 (90.73) 0.555
protime (mean (sd)) 10.65 (0.85) 10.80 (1.14) 0.197
stage (%) 0.205 exact
1 12 ( 7.6) 4 ( 2.6)
2 35 (22.2) 32 (20.8)
3 56 (35.4) 64 (41.6)
4 55 (34.8) 54 (35.1)
You may want to copy and paste it to Excel like I often have to do. Use the quote argument to add quotes to avoid mishandling of spaces. After pasting in Excel, use the Text Import Wizard, and just press Finish.
print(tableOne, nonnormal = c("bili","chol","copper","alk.phos","trig"),
exact = c("status","stage"), cramVars = "sex", quote = TRUE)
"Stratified by trt"
"" "1" "2" "p" "test"
"n" "158" "154" "" ""
"time (mean (sd))" "2015.62 (1094.12)" "1996.86 (1155.93)" " 0.883" ""
"status (%)" " " " " " 0.884" "exact"
" 0" " 83 (52.5) " " 85 (55.2) " "" ""
" 1" " 10 ( 6.3) " " 9 ( 5.8) " "" ""
" 2" " 65 (41.1) " " 60 (39.0) " "" ""
"age (mean (sd))" " 51.42 (11.01)" " 48.58 (9.96)" " 0.018" ""
"sex = m/f (%)" " 21/137 (13.3/86.7) " " 15/139 (9.7/90.3) " " 0.421" ""
"ascites = 1 (%)" " 14 (8.9) " " 10 (6.5) " " 0.567" ""
"hepato = 1 (%)" " 73 (46.2) " " 87 (56.5) " " 0.088" ""
"spiders = 1 (%)" " 45 (28.5) " " 45 (29.2) " " 0.985" ""
"edema (%)" " " " " " 0.877" ""
" 0" " 132 (83.5) " " 131 (85.1) " "" ""
" 0.5" " 16 (10.1) " " 13 ( 8.4) " "" ""
" 1" " 10 ( 6.3) " " 10 ( 6.5) " "" ""
"bili (median [IQR])" " 1.40 [0.80, 3.20]" " 1.30 [0.72, 3.60]" " 0.842" "nonnorm"
"chol (median [IQR])" " 315.50 [247.75, 417.00]" " 303.50 [254.25, 377.00]" " 0.544" "nonnorm"
"albumin (mean (sd))" " 3.52 (0.44)" " 3.52 (0.40)" " 0.874" ""
"copper (median [IQR])" " 73.00 [40.00, 121.00]" " 73.00 [43.00, 139.00]" " 0.717" "nonnorm"
"alk.phos (median [IQR])" "1214.50 [840.75, 2028.00]" "1283.00 [922.50, 1949.75]" " 0.812" "nonnorm"
"ast (mean (sd))" " 120.21 (54.52)" " 124.97 (58.93)" " 0.460" ""
"trig (median [IQR])" " 106.00 [84.50, 146.00]" " 113.00 [84.50, 155.00]" " 0.370" "nonnorm"
"platelet (mean (sd))" " 258.75 (100.32)" " 265.20 (90.73)" " 0.555" ""
"protime (mean (sd))" " 10.65 (0.85)" " 10.80 (1.14)" " 0.197" ""
"stage (%)" " " " " " 0.205" "exact"
" 1" " 12 ( 7.6) " " 4 ( 2.6) " "" ""
" 2" " 35 (22.2) " " 32 (20.8) " "" ""
" 3" " 56 (35.4) " " 64 (41.6) " "" ""
" 4" " 55 (34.8) " " 54 (35.1) " "" ""