library(tableone)
library(survival)
#find the data using
head(pbc)
## id time status trt age sex ascites hepato spiders edema bili chol
## 1 1 400 2 1 58.76523 f 1 1 1 1.0 14.5 261
## 2 2 4500 0 1 56.44627 f 0 1 1 0.0 1.1 302
## 3 3 1012 2 1 70.07255 m 0 0 0 0.5 1.4 176
## 4 4 1925 2 1 54.74059 f 0 1 1 0.5 1.8 244
## 5 5 1504 1 2 38.10541 f 0 1 1 0.0 3.4 279
## 6 6 2503 2 2 66.25873 f 0 1 0 0.0 0.8 248
## albumin copper alk.phos ast trig platelet protime stage
## 1 2.60 156 1718.0 137.95 172 190 12.2 4
## 2 4.14 54 7394.8 113.52 88 221 10.6 3
## 3 3.48 210 516.0 96.10 55 151 12.0 4
## 4 2.54 64 6121.8 60.63 92 183 10.3 4
## 5 3.53 143 671.0 113.15 72 136 10.9 3
## 6 3.98 50 944.0 93.00 63 NA 11.0 3
#create basic table
t1 <- CreateTableOne(data=pbc)
## Warning: replacing previous import 'lifecycle::last_warnings' by
## 'rlang::last_warnings' when loading 'tibble'
## Warning: replacing previous import 'lifecycle::last_warnings' by
## 'rlang::last_warnings' when loading 'pillar'
## Warning: replacing previous import 'lifecycle::last_warnings' by
## 'rlang::last_warnings' when loading 'hms'
print(t1)
##
## Overall
## n 418
## id (mean (SD)) 209.50 (120.81)
## time (mean (SD)) 1917.78 (1104.67)
## status (mean (SD)) 0.83 (0.96)
## trt (mean (SD)) 1.49 (0.50)
## age (mean (SD)) 50.74 (10.45)
## sex = f (%) 374 (89.5)
## ascites (mean (SD)) 0.08 (0.27)
## hepato (mean (SD)) 0.51 (0.50)
## spiders (mean (SD)) 0.29 (0.45)
## edema (mean (SD)) 0.10 (0.25)
## 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 (mean (SD)) 3.02 (0.88)
#create table with factor variables
fvar <- c("trt","status","ascites","stage")
uvar <- c("time","status","trt","age","sex","ascites","bili","edema","ast","stage")
#create table with different groups
t2 <- CreateTableOne(data = pbc,vars = uvar,factorVars = fvar)
print(t2)
##
## Overall
## n 418
## time (mean (SD)) 1917.78 (1104.67)
## status (%)
## 0 232 (55.5)
## 1 25 ( 6.0)
## 2 161 (38.5)
## trt = 2 (%) 154 (49.4)
## age (mean (SD)) 50.74 (10.45)
## sex = f (%) 374 (89.5)
## ascites = 1 (%) 24 ( 7.7)
## bili (mean (SD)) 3.22 (4.41)
## edema (mean (SD)) 0.10 (0.25)
## ast (mean (SD)) 122.56 (56.70)
## stage (%)
## 1 21 ( 5.1)
## 2 92 (22.3)
## 3 155 (37.6)
## 4 144 (35.0)
#create table for specified variable"trt"
t3 <- CreateTableOne(data = pbc,vars = uvar,factorVars = fvar,strata = "trt")
print(t3)
## 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)
## trt = 2 (%) 0 ( 0.0) 154 (100.0) <0.001
## 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
## bili (mean (SD)) 2.87 (3.63) 3.65 (5.28) 0.131
## edema (mean (SD)) 0.11 (0.28) 0.11 (0.27) 0.828
## ast (mean (SD)) 120.21 (54.52) 124.97 (58.93) 0.460
## 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)
1.Data source: I use the dataset from the survival package named “pbc”.Primary sclerosing cholangitis is an autoimmune disease leading to destruction of the small bile ducts in the liver and this data is from the Mayo Clinic trial in PBC conducted between 1974 and 1984 including 424 PBC patients.
2.Convey: I want to display the summary of key statistic value (such as mean,standard deviation,p-value for testing) for these variables in one table clearly.
3.Function: For the first table, it is only the basic function “CreateTableOne” and there are some useless outputs and the second table will improve this. For the last table, I add the function “strata” and I put the key variable “trt”(treatment)into it and it gives out the test result clearly and easy for making analysis.
4.Formatting: The first table is the basic one and I format with only putting the data into it. The second table separates the factor variables and the last table looks pretty the statistic test and gives out the p-value.