1. VÍ DỤ 1

Load các gói cần thiết

require(tidyverse)
require(tableone)
library(survival)
library(datasets)

Tìm hiểu hàm CreateTableOne

?CreateTableOne

Ví dụ minh họa như trong Help

data(pbc)
## Check variables
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
## Make categorical variables factors
factorVars <- c("status","ascites","edema","stage")

## Create a variable list

vars <- c("time","status","age","sex","ascites","hepato",
          "spiders","edema","bili","chol", "stage")

## Create Table 1 stratified by trt
tableOne <- CreateTableOne(vars = vars, factorVars = factorVars,
                           data = pbc)
tableOne
##                      
##                       Overall          
##   n                       418          
##   time (mean (SD))    1917.78 (1104.67)
##   status (%)                           
##      0                    232 (55.5)   
##      1                     25 ( 6.0)   
##      2                    161 (38.5)   
##   age (mean (SD))       50.74 (10.45)  
##   sex = f (%)             374 (89.5)   
##   ascites = 1 (%)          24 ( 7.7)   
##   hepato (mean (SD))     0.51 (0.50)   
##   spiders (mean (SD))    0.29 (0.45)   
##   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) 
##   stage (%)                            
##      1                     21 ( 5.1)   
##      2                     92 (22.3)   
##      3                    155 (37.6)   
##      4                    144 (35.0)

Xuất kết quả ra file .csv theo tùy chọn

#-----to write in .csv file
tableOneALL <- print(tableOne)
##                      
##                       Overall          
##   n                       418          
##   time (mean (SD))    1917.78 (1104.67)
##   status (%)                           
##      0                    232 (55.5)   
##      1                     25 ( 6.0)   
##      2                    161 (38.5)   
##   age (mean (SD))       50.74 (10.45)  
##   sex = f (%)             374 (89.5)   
##   ascites = 1 (%)          24 ( 7.7)   
##   hepato (mean (SD))     0.51 (0.50)   
##   spiders (mean (SD))    0.29 (0.45)   
##   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) 
##   stage (%)                            
##      1                     21 ( 5.1)   
##      2                     92 (22.3)   
##      3                    155 (37.6)   
##      4                    144 (35.0)
tableOnef <- print(tableOne, format = "f")
##                      
##                       Overall          
##   n                       418          
##   time (mean (SD))    1917.78 (1104.67)
##   status                               
##      0                    232          
##      1                     25          
##      2                    161          
##   age (mean (SD))       50.74 (10.45)  
##   sex = f                 374          
##   ascites = 1              24          
##   hepato (mean (SD))     0.51 (0.50)   
##   spiders (mean (SD))    0.29 (0.45)   
##   edema                                
##      0                    354          
##      0.5                   44          
##      1                     20          
##   bili (mean (SD))       3.22 (4.41)   
##   chol (mean (SD))     369.51 (231.94) 
##   stage                                
##      1                     21          
##      2                     92          
##      3                    155          
##      4                    144
tableOnep <- print(tableOne, format = "p")
##                      
##                       Overall          
##   n                       418          
##   time (mean (SD))    1917.78 (1104.67)
##   status (%)                           
##      0                   55.5          
##      1                    6.0          
##      2                   38.5          
##   age (mean (SD))       50.74 (10.45)  
##   sex = f (%)            89.5          
##   ascites = 1 (%)         7.7          
##   hepato (mean (SD))     0.51 (0.50)   
##   spiders (mean (SD))    0.29 (0.45)   
##   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) 
##   stage (%)                            
##      1                    5.1          
##      2                   22.3          
##      3                   37.6          
##      4                   35.0
setwd("D:/Tap huan VIASM/VIDEO/TableOne")
write.csv(tableOneALL, file = "tableOneALL.csv")
write.csv(tableOnef, file = "tableOnef.csv")
write.csv(tableOnep, file = "tableOnep.csv")

VÍ DỤ 2

Copy các tham số của Ví dụ 1 và điều chỉnh tương ứng với data ở ví dụ 2

#---------------So lieu gom ca bien lien tuc va bien roi rac
head(iris)
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
## 2          4.9         3.0          1.4         0.2  setosa
## 3          4.7         3.2          1.3         0.2  setosa
## 4          4.6         3.1          1.5         0.2  setosa
## 5          5.0         3.6          1.4         0.2  setosa
## 6          5.4         3.9          1.7         0.4  setosa
names(iris)
## [1] "Sepal.Length" "Sepal.Width"  "Petal.Length" "Petal.Width"  "Species"
## Make categorical variables factors
factorVars <- c("Species"  )

## Create a variable list
vars <- c("Sepal.Length" ,"Sepal.Width",  "Petal.Length", "Petal.Width", "Species")
## Create Table 1 stratified by trt
tableIris <- CreateTableOne(vars = vars, factorVars  = factorVars , 
                            data = iris)
tableIris
##                           
##                            Overall     
##   n                         150        
##   Sepal.Length (mean (SD)) 5.84 (0.83) 
##   Sepal.Width (mean (SD))  3.06 (0.44) 
##   Petal.Length (mean (SD)) 3.76 (1.77) 
##   Petal.Width (mean (SD))  1.20 (0.76) 
##   Species (%)                          
##      setosa                  50 (33.3) 
##      versicolor              50 (33.3) 
##      virginica               50 (33.3)

Lưu kết quả bảng dưới file .csv và tùy chọn

#-----to write in .csv file
tableIrisALL <- print(tableIris)
##                           
##                            Overall     
##   n                         150        
##   Sepal.Length (mean (SD)) 5.84 (0.83) 
##   Sepal.Width (mean (SD))  3.06 (0.44) 
##   Petal.Length (mean (SD)) 3.76 (1.77) 
##   Petal.Width (mean (SD))  1.20 (0.76) 
##   Species (%)                          
##      setosa                  50 (33.3) 
##      versicolor              50 (33.3) 
##      virginica               50 (33.3)
tableIrisf <- print(tableIris, format = "f")
##                           
##                            Overall    
##   n                         150       
##   Sepal.Length (mean (SD)) 5.84 (0.83)
##   Sepal.Width (mean (SD))  3.06 (0.44)
##   Petal.Length (mean (SD)) 3.76 (1.77)
##   Petal.Width (mean (SD))  1.20 (0.76)
##   Species                             
##      setosa                  50       
##      versicolor              50       
##      virginica               50
tableIrisp <- print(tableIris, format = "p")
##                           
##                            Overall    
##   n                         150       
##   Sepal.Length (mean (SD)) 5.84 (0.83)
##   Sepal.Width (mean (SD))  3.06 (0.44)
##   Petal.Length (mean (SD)) 3.76 (1.77)
##   Petal.Width (mean (SD))  1.20 (0.76)
##   Species (%)                         
##      setosa                33.3       
##      versicolor            33.3       
##      virginica             33.3
write.csv(tableIrisALL, file = "tableIrisALL.csv")
write.csv(tableIrisf, file = "tableIrisf.csv")
write.csv(tableIrisp, file = "tableIrisp.csv")