46  tableone绘制三线表

临床研究中基线资料表必不可少,通常也是你文章中的第一张表!

一般我们会通过Word或者Excel进行绘制基线资料表,但是这种方法很麻烦,需要不停的复制粘贴。。。

今天介绍的这个tableone,就是专门为这个基线资料表而生的!和之前介绍过的comparegroups有点像哦~但是功能不止于基线资料表,还可以做非常多统计描述的工作!比如前面介绍倾向性评分匹配时就用到了这个包。

支持连续性变量及分类变量,支持自动标注P值,加权数据也是支持的,真的是一步到位了!

46.1 安装

两种安装方式任选一种即可:

install.packages("tableone")
# install.packages("devtools")
devtools::install_github(repo = "kaz-yos/tableone", ref = "develop")

R包安装有问题的小伙伴可以加我微信或者评论区留言~

46.2 使用

为了方便比较,还是使用compareGroups包自带的regicor数据集。

为了说明这个软件包是如何工作的,我们从REGICOR研究中取了一部分数据。REGICOR是一个 对来自西班牙东北部的参与者进行的横断面研究,包括:人口统计学信息(年龄、性别、身高、体重、腰围等)、血脂特征(总胆固醇和胆固醇、甘油三酯等)、问卷调查信息(体育活动,生活质量,…)等。此外,心血管事件和 死亡信息来自医院和官方登记处。

library(compareGroups)
library(tableone)

data("regicor")
dim(regicor)
## [1] 2294   25
str(regicor)
## 'data.frame':    2294 obs. of  25 variables:
##  $ id      : num  2.26e+03 1.88e+03 3.00e+09 3.00e+09 3.00e+09 ...
##   ..- attr(*, "label")= Named chr "Individual id"
##   .. ..- attr(*, "names")= chr "id"
##  $ year    : Factor w/ 3 levels "1995","2000",..: 3 3 2 2 2 2 2 1 3 1 ...
##   ..- attr(*, "label")= Named chr "Recruitment year"
##   .. ..- attr(*, "names")= chr "year"
##  $ age     : int  70 56 37 69 70 40 66 53 43 70 ...
##   ..- attr(*, "label")= Named chr "Age"
##   .. ..- attr(*, "names")= chr "age"
##  $ sex     : Factor w/ 2 levels "Male","Female": 2 2 1 2 2 2 1 2 2 1 ...
##   ..- attr(*, "label")= chr "Sex"
##  $ smoker  : Factor w/ 3 levels "Never smoker",..: 1 1 2 1 NA 2 1 1 3 3 ...
##   ..- attr(*, "label")= Named chr "Smoking status"
##   .. ..- attr(*, "names")= chr "smoker"
##  $ sbp     : int  138 139 132 168 NA 108 120 132 95 142 ...
##   ..- attr(*, "label")= Named chr "Systolic blood pressure"
##   .. ..- attr(*, "names")= chr "sbp"
##  $ dbp     : int  75 89 82 97 NA 70 72 78 65 78 ...
##   ..- attr(*, "label")= Named chr "Diastolic blood pressure"
##   .. ..- attr(*, "names")= chr "dbp"
##  $ histhtn : Factor w/ 2 levels "Yes","No": 2 2 2 2 2 2 1 2 2 2 ...
##   ..- attr(*, "label")= Named chr "History of hypertension"
##   .. ..- attr(*, "names")= chr "histbp"
##  $ txhtn   : Factor w/ 2 levels "No","Yes": 1 1 1 1 1 1 2 1 1 1 ...
##   ..- attr(*, "label")= chr "Hypertension treatment"
##  $ chol    : num  294 220 245 168 NA NA 298 254 194 188 ...
##   ..- attr(*, "label")= Named chr "Total cholesterol"
##   .. ..- attr(*, "names")= chr "chol"
##  $ hdl     : num  57 50 59.8 53.2 NA ...
##   ..- attr(*, "label")= Named chr "HDL cholesterol"
##   .. ..- attr(*, "names")= chr "hdl"
##  $ triglyc : num  93 160 89 116 NA 94 71 NA 68 137 ...
##   ..- attr(*, "label")= Named chr "Triglycerides"
##   .. ..- attr(*, "names")= chr "triglyc"
##  $ ldl     : num  218.4 138 167.4 91.6 NA ...
##   ..- attr(*, "label")= Named chr "LDL cholesterol"
##   .. ..- attr(*, "names")= chr "ldl"
##  $ histchol: Factor w/ 2 levels "Yes","No": 2 2 2 2 NA 2 1 2 2 2 ...
##   ..- attr(*, "label")= chr "History of hyperchol."
##  $ txchol  : Factor w/ 2 levels "No","Yes": 1 1 1 1 NA 1 1 1 1 1 ...
##   ..- attr(*, "label")= Named chr "Cholesterol treatment"
##   .. ..- attr(*, "names")= chr "txchol"
##  $ height  : num  160 163 170 147 NA ...
##   ..- attr(*, "label")= Named chr "Height (cm)"
##   .. ..- attr(*, "names")= chr "height"
##  $ weight  : num  64 67 70 68 NA 43.5 79.2 45.8 53 62 ...
##   ..- attr(*, "label")= Named chr "Weight (Kg)"
##   .. ..- attr(*, "names")= chr "weight"
##  $ bmi     : num  25 25.2 24.2 31.5 NA ...
##   ..- attr(*, "label")= Named chr "Body mass index"
##   .. ..- attr(*, "names")= chr "bmi"
##  $ phyact  : num  304 160 553 522 NA ...
##   ..- attr(*, "label")= Named chr "Physical activity (Kcal/week)"
##   .. ..- attr(*, "names")= chr "phyact"
##  $ pcs     : num  54.5 58.2 43.4 54.3 NA ...
##   ..- attr(*, "label")= Named chr "Physical component"
##   .. ..- attr(*, "names")= chr "pcs"
##  $ mcs     : num  58.9 48 62.6 57.9 NA ...
##   ..- attr(*, "label")= chr "Mental component"
##  $ cv      : Factor w/ 2 levels "No","Yes": 1 1 1 1 NA 1 1 1 1 1 ...
##   ..- attr(*, "label")= chr "Cardiovascular event"
##  $ tocv    : num  1025 2757 1906 1055 NA ...
##   ..- attr(*, "label")= chr "Days to cardiovascular event or end of follow-up"
##  $ death   : Factor w/ 2 levels "No","Yes": 2 1 1 1 NA 1 2 1 1 1 ...
##   ..- attr(*, "label")= chr "Overall death"
##  $ todeath : num  1299.2 39.3 858.4 1833.1 NA ...
##   ..- attr(*, "label")= chr "Days to overall death or end of follow-up"

各个变量的信息如下:

Name Label Codes
id Individual id
year Recruitment year 1995; 2000; 2005
age Age
sex Sex Male; Female
smoker Smoking status Never smoker; Current or former < 1y; Former $\geq$ 1y
sbp Systolic blood pressure
dbp Diastolic blood pressure
histhtn History of hypertension Yes; No
txhtn Hypertension treatment No; Yes
chol Total cholesterol
hdl HDL cholesterol
triglyc Triglycerides
ldl LDL cholesterol
histchol History of hyperchol. Yes; No
txchol Cholesterol treatment No; Yes
height Height (cm)
weight Weight (Kg)
bmi Body mass index
phyact Physical activity (Kcal/week)
pcs Physical component
mcs Mental component
cv Cardiovascular event No; Yes
tocv Days to cardiovascular event or end of follow-up
death Overall death No; Yes
todeath Days to overall death or end of follow-up

为了防止这个数据集再次消失,把它保存到本地。。。

save(regicor,file = "./datasets/regicor.rdata")

46.2.1 基本描述功能

首先是基本的统计描述功能,使用CreateTableOne()函数可以给出数据的基本情况:

tab <- CreateTableOne(data = regicor)
tab
##                            
##                             Overall                      
##   n                                  2294                
##   id (mean (SD))            1215817623.64 (1339538685.65)
##   year (%)                                               
##      1995                             431 (18.8)         
##      2000                             786 (34.3)         
##      2005                            1077 (46.9)         
##   age (mean (SD))                   54.74 (11.05)        
##   sex = Female (%)                   1193 (52.0)         
##   smoker (%)                                             
##      Never smoker                    1201 (53.8)         
##      Current or former < 1y           593 (26.6)         
##      Former >= 1y                     439 (19.7)         
##   sbp (mean (SD))                  131.17 (20.31)        
##   dbp (mean (SD))                   79.66 (10.55)        
##   histhtn = No (%)                   1563 (68.4)         
##   txhtn = Yes (%)                     428 (19.0)         
##   chol (mean (SD))                 218.76 (45.25)        
##   hdl (mean (SD))                   52.69 (14.75)        
##   triglyc (mean (SD))              115.58 (73.94)        
##   ldl (mean (SD))                  143.25 (39.69)        
##   histchol = No (%)                  1564 (68.8)         
##   txchol = Yes (%)                    228 (10.2)         
##   height (mean (SD))               162.92 (9.22)         
##   weight (mean (SD))                73.44 (13.68)        
##   bmi (mean (SD))                   27.64 (4.56)         
##   phyact (mean (SD))               398.83 (388.16)       
##   pcs (mean (SD))                   49.62 (9.01)         
##   mcs (mean (SD))                   47.98 (10.98)        
##   cv = Yes (%)                         92 ( 4.3)         
##   tocv (mean (SD))                1754.67 (1080.62)      
##   death = Yes (%)                     173 ( 8.1)         
##   todeath (mean (SD))             1721.31 (1051.05)

46.2.2 选择变量&指定变量类型

通过vars()函数指定保留哪些变量,通过factorVars()函数指定哪些是分类变量。

但其实regicor这个数据集已经把分类变量因子化了,因此这里不用factorVars()也是可以的。

CreateTableOne(data = regicor,
               vars = c("age","sex","bmi","smoker","height","weight"),
               factorVars = c("smoker","sex")
               )
##                            
##                             Overall       
##   n                           2294        
##   age (mean (SD))            54.74 (11.05)
##   sex = Female (%)            1193 (52.0) 
##   bmi (mean (SD))            27.64 (4.56) 
##   smoker (%)                              
##      Never smoker             1201 (53.8) 
##      Current or former < 1y    593 (26.6) 
##      Former >= 1y              439 (19.7) 
##   height (mean (SD))        162.92 (9.22) 
##   weight (mean (SD))         73.44 (13.68)

46.2.3 显示所有水平

在第一个表中我们可以发现很多分类变量被折叠了,都只显示了yes或者no的一部分,比如sex/txhtn/cv等,我们在print()中添加showAllLevels = T显示所有分类!

print(tab, showAllLevels = T)
##                      
##                       level                  Overall                      
##   n                                                   2294                
##   id (mean (SD))                             1215817623.64 (1339538685.65)
##   year (%)            1995                             431 (18.8)         
##                       2000                             786 (34.3)         
##                       2005                            1077 (46.9)         
##   age (mean (SD))                                    54.74 (11.05)        
##   sex (%)             Male                            1101 (48.0)         
##                       Female                          1193 (52.0)         
##   smoker (%)          Never smoker                    1201 (53.8)         
##                       Current or former < 1y           593 (26.6)         
##                       Former >= 1y                     439 (19.7)         
##   sbp (mean (SD))                                   131.17 (20.31)        
##   dbp (mean (SD))                                    79.66 (10.55)        
##   histhtn (%)         Yes                              723 (31.6)         
##                       No                              1563 (68.4)         
##   txhtn (%)           No                              1823 (81.0)         
##                       Yes                              428 (19.0)         
##   chol (mean (SD))                                  218.76 (45.25)        
##   hdl (mean (SD))                                    52.69 (14.75)        
##   triglyc (mean (SD))                               115.58 (73.94)        
##   ldl (mean (SD))                                   143.25 (39.69)        
##   histchol (%)        Yes                              709 (31.2)         
##                       No                              1564 (68.8)         
##   txchol (%)          No                              2011 (89.8)         
##                       Yes                              228 (10.2)         
##   height (mean (SD))                                162.92 (9.22)         
##   weight (mean (SD))                                 73.44 (13.68)        
##   bmi (mean (SD))                                    27.64 (4.56)         
##   phyact (mean (SD))                                398.83 (388.16)       
##   pcs (mean (SD))                                    49.62 (9.01)         
##   mcs (mean (SD))                                    47.98 (10.98)        
##   cv (%)              No                              2071 (95.7)         
##                       Yes                               92 ( 4.3)         
##   tocv (mean (SD))                                 1754.67 (1080.62)      
##   death (%)           No                              1975 (91.9)         
##                       Yes                              173 ( 8.1)         
##   todeath (mean (SD))                              1721.31 (1051.05)

对于二分类变量,还可以使用cramVars参数达到类似的效果,但要注意,此时不同类别是显示在一行中的:

print(tab, cramVars = c("sex","histchol","txchol","cv","death"))
##                            
##                             Overall                      
##   n                                  2294                
##   id (mean (SD))            1215817623.64 (1339538685.65)
##   year (%)                                               
##      1995                             431 (18.8)         
##      2000                             786 (34.3)         
##      2005                            1077 (46.9)         
##   age (mean (SD))                   54.74 (11.05)        
##   sex = Male/Female (%)         1101/1193 (48.0/52.0)    
##   smoker (%)                                             
##      Never smoker                    1201 (53.8)         
##      Current or former < 1y           593 (26.6)         
##      Former >= 1y                     439 (19.7)         
##   sbp (mean (SD))                  131.17 (20.31)        
##   dbp (mean (SD))                   79.66 (10.55)        
##   histhtn = No (%)                   1563 (68.4)         
##   txhtn = Yes (%)                     428 (19.0)         
##   chol (mean (SD))                 218.76 (45.25)        
##   hdl (mean (SD))                   52.69 (14.75)        
##   triglyc (mean (SD))              115.58 (73.94)        
##   ldl (mean (SD))                  143.25 (39.69)        
##   histchol = Yes/No (%)          709/1564 (31.2/68.8)    
##   txchol = No/Yes (%)            2011/228 (89.8/10.2)    
##   height (mean (SD))               162.92 (9.22)         
##   weight (mean (SD))                73.44 (13.68)        
##   bmi (mean (SD))                   27.64 (4.56)         
##   phyact (mean (SD))               398.83 (388.16)       
##   pcs (mean (SD))                   49.62 (9.01)         
##   mcs (mean (SD))                   47.98 (10.98)        
##   cv = No/Yes (%)                 2071/92 (95.7/4.3)     
##   tocv (mean (SD))                1754.67 (1080.62)      
##   death = No/Yes (%)             1975/173 (91.9/8.1)     
##   todeath (mean (SD))             1721.31 (1051.05)

46.2.4 非正态分布变量

对于正态分布的变量使用的是均值±标准差的方式进行展示,对于非正态变量则通过中位数(四分位距)表示。可以通过nonnormal参数指定。

print(tab, 
      showAllLevels = T,
      nonnormal = c("todeath")
      )
##                         
##                          level                  Overall                        
##   n                                                      2294                  
##   id (mean (SD))                                1215817623.64 (1339538685.65)  
##   year (%)               1995                             431 (18.8)           
##                          2000                             786 (34.3)           
##                          2005                            1077 (46.9)           
##   age (mean (SD))                                       54.74 (11.05)          
##   sex (%)                Male                            1101 (48.0)           
##                          Female                          1193 (52.0)           
##   smoker (%)             Never smoker                    1201 (53.8)           
##                          Current or former < 1y           593 (26.6)           
##                          Former >= 1y                     439 (19.7)           
##   sbp (mean (SD))                                      131.17 (20.31)          
##   dbp (mean (SD))                                       79.66 (10.55)          
##   histhtn (%)            Yes                              723 (31.6)           
##                          No                              1563 (68.4)           
##   txhtn (%)              No                              1823 (81.0)           
##                          Yes                              428 (19.0)           
##   chol (mean (SD))                                     218.76 (45.25)          
##   hdl (mean (SD))                                       52.69 (14.75)          
##   triglyc (mean (SD))                                  115.58 (73.94)          
##   ldl (mean (SD))                                      143.25 (39.69)          
##   histchol (%)           Yes                              709 (31.2)           
##                          No                              1564 (68.8)           
##   txchol (%)             No                              2011 (89.8)           
##                          Yes                              228 (10.2)           
##   height (mean (SD))                                   162.92 (9.22)           
##   weight (mean (SD))                                    73.44 (13.68)          
##   bmi (mean (SD))                                       27.64 (4.56)           
##   phyact (mean (SD))                                   398.83 (388.16)         
##   pcs (mean (SD))                                       49.62 (9.01)           
##   mcs (mean (SD))                                       47.98 (10.98)          
##   cv (%)                 No                              2071 (95.7)           
##                          Yes                               92 ( 4.3)           
##   tocv (mean (SD))                                    1754.67 (1080.62)        
##   death (%)              No                              1975 (91.9)           
##                          Yes                              173 ( 8.1)           
##   todeath (median [IQR])                              1668.40 [787.63, 2662.54]

46.2.5 分层显示

但是在实际写论文的时候,经常需要分组显示,分别展示不同组间的统计资料,然后计算组间有没有差别!

可以通过strata参数实现,自动给出P值。

tab_s <- CreateTableOne(data = regicor, 
                        vars = colnames(regicor)[-1], # id就不要了 
                        strata = "year")
# 全部展开展示:
print(tab_s, showAllLevels = T)
##                      Stratified by year
##                       level                  1995             
##   n                                              431          
##   year (%)            1995                       431 (100.0)  
##                       2000                         0 (  0.0)  
##                       2005                         0 (  0.0)  
##   age (mean (SD))                              54.10 (11.72)  
##   sex (%)             Male                       206 ( 47.8)  
##                       Female                     225 ( 52.2)  
##   smoker (%)          Never smoker               234 ( 56.4)  
##                       Current or former < 1y     109 ( 26.3)  
##                       Former >= 1y                72 ( 17.3)  
##   sbp (mean (SD))                             132.61 (19.17)  
##   dbp (mean (SD))                              77.04 (10.54)  
##   histhtn (%)         Yes                        111 ( 25.8)  
##                       No                         320 ( 74.2)  
##   txhtn (%)           No                         360 ( 83.5)  
##                       Yes                         71 ( 16.5)  
##   chol (mean (SD))                            225.32 (43.13)  
##   hdl (mean (SD))                              51.87 (14.46)  
##   triglyc (mean (SD))                         114.15 (74.37)  
##   ldl (mean (SD))                             151.73 (38.41)  
##   histchol (%)        Yes                         97 ( 22.5)  
##                       No                         334 ( 77.5)  
##   txchol (%)          No                         403 ( 93.5)  
##                       Yes                         28 (  6.5)  
##   height (mean (SD))                          163.50 (9.21)   
##   weight (mean (SD))                           72.29 (12.61)  
##   bmi (mean (SD))                              27.02 (4.15)   
##   phyact (mean (SD))                          490.78 (419.04) 
##   pcs (mean (SD))                              49.33 (8.08)   
##   mcs (mean (SD))                              49.25 (11.35)  
##   cv (%)              No                         388 ( 97.5)  
##                       Yes                         10 (  2.5)  
##   tocv (mean (SD))                           1783.62 (1101.17)
##   death (%)           No                         369 ( 95.3)  
##                       Yes                         18 (  4.7)  
##   todeath (mean (SD))                        1713.31 (1042.18)
##                      Stratified by year
##                       2000              2005              p      test
##   n                       786              1077                      
##   year (%)                  0 (  0.0)         0 (  0.0)   <0.001     
##                           786 (100.0)         0 (  0.0)              
##                             0 (  0.0)      1077 (100.0)              
##   age (mean (SD))       54.34 (11.22)     55.28 (10.63)    0.078     
##   sex (%)                 390 ( 49.6)       505 ( 46.9)    0.506     
##                           396 ( 50.4)       572 ( 53.1)              
##   smoker (%)              414 ( 54.6)       553 ( 52.2)   <0.001     
##                           267 ( 35.2)       217 ( 20.5)              
##                            77 ( 10.2)       290 ( 27.4)              
##   sbp (mean (SD))      133.04 (21.31)    129.26 (19.85)   <0.001     
##   dbp (mean (SD))       80.80 (10.31)     79.88 (10.55)   <0.001     
##   histhtn (%)             233 ( 29.6)       379 ( 35.5)   <0.001     
##                           553 ( 70.4)       690 ( 64.5)              
##   txhtn (%)               659 ( 83.8)       804 ( 77.8)    0.002     
##                           127 ( 16.2)       230 ( 22.2)              
##   chol (mean (SD))     223.67 (44.37)    213.03 (45.92)   <0.001     
##   hdl (mean (SD))       52.34 (15.60)     53.24 (14.23)    0.208     
##   triglyc (mean (SD))  113.94 (70.69)    117.27 (76.01)    0.582     
##   ldl (mean (SD))      149.03 (38.61)    136.32 (39.68)   <0.001     
##   histchol (%)            256 ( 33.2)       356 ( 33.2)   <0.001     
##                           515 ( 66.8)       715 ( 66.8)              
##   txchol (%)              705 ( 91.2)       903 ( 87.2)   <0.001     
##                            68 (  8.8)       132 ( 12.8)              
##   height (mean (SD))   162.01 (9.39)     163.34 (9.05)     0.003     
##   weight (mean (SD))    73.84 (13.95)     73.60 (13.87)    0.150     
##   bmi (mean (SD))       28.10 (4.62)      27.56 (4.63)    <0.001     
##   phyact (mean (SD))   421.74 (377.13)   351.16 (378.05)  <0.001     
##   pcs (mean (SD))       49.01 (9.63)      50.14 (8.91)     0.032     
##   mcs (mean (SD))       48.90 (10.95)     46.87 (10.75)   <0.001     
##   cv (%)                  706 ( 95.3)       977 ( 95.4)    0.161     
##                            35 (  4.7)        47 (  4.6)              
##   tocv (mean (SD))    1685.62 (1079.72) 1793.38 (1071.81)  0.099     
##   death (%)               657 ( 89.0)       949 ( 92.8)   <0.001     
##                            81 ( 11.0)        74 (  7.2)              
##   todeath (mean (SD)) 1674.37 (1050.09) 1758.19 (1054.68)  0.252

但是tableone并没有提供直接导出到Word的途径,只能导入到csv文件中,这是有点差劲的地方。

46.2.6 导出

tab_sv <- print(tab_s,showAllLevels = T,printToggle = F)

write.csv(tab_sv,file = "tab_sv.csv")

46.3 不同包的比较

可以看到tableone做出一张表需要2行代码:

tab_s <- CreateTableOne(data = regicor,vars = colnames(regicor)[-1], 
                        strata = "year")
print(tab_s,showAllLevels = T)
##                      Stratified by year
##                       level                  1995             
##   n                                              431          
##   year (%)            1995                       431 (100.0)  
##                       2000                         0 (  0.0)  
##                       2005                         0 (  0.0)  
##   age (mean (SD))                              54.10 (11.72)  
##   sex (%)             Male                       206 ( 47.8)  
##                       Female                     225 ( 52.2)  
##   smoker (%)          Never smoker               234 ( 56.4)  
##                       Current or former < 1y     109 ( 26.3)  
##                       Former >= 1y                72 ( 17.3)  
##   sbp (mean (SD))                             132.61 (19.17)  
##   dbp (mean (SD))                              77.04 (10.54)  
##   histhtn (%)         Yes                        111 ( 25.8)  
##                       No                         320 ( 74.2)  
##   txhtn (%)           No                         360 ( 83.5)  
##                       Yes                         71 ( 16.5)  
##   chol (mean (SD))                            225.32 (43.13)  
##   hdl (mean (SD))                              51.87 (14.46)  
##   triglyc (mean (SD))                         114.15 (74.37)  
##   ldl (mean (SD))                             151.73 (38.41)  
##   histchol (%)        Yes                         97 ( 22.5)  
##                       No                         334 ( 77.5)  
##   txchol (%)          No                         403 ( 93.5)  
##                       Yes                         28 (  6.5)  
##   height (mean (SD))                          163.50 (9.21)   
##   weight (mean (SD))                           72.29 (12.61)  
##   bmi (mean (SD))                              27.02 (4.15)   
##   phyact (mean (SD))                          490.78 (419.04) 
##   pcs (mean (SD))                              49.33 (8.08)   
##   mcs (mean (SD))                              49.25 (11.35)  
##   cv (%)              No                         388 ( 97.5)  
##                       Yes                         10 (  2.5)  
##   tocv (mean (SD))                           1783.62 (1101.17)
##   death (%)           No                         369 ( 95.3)  
##                       Yes                         18 (  4.7)  
##   todeath (mean (SD))                        1713.31 (1042.18)
##                      Stratified by year
##                       2000              2005              p      test
##   n                       786              1077                      
##   year (%)                  0 (  0.0)         0 (  0.0)   <0.001     
##                           786 (100.0)         0 (  0.0)              
##                             0 (  0.0)      1077 (100.0)              
##   age (mean (SD))       54.34 (11.22)     55.28 (10.63)    0.078     
##   sex (%)                 390 ( 49.6)       505 ( 46.9)    0.506     
##                           396 ( 50.4)       572 ( 53.1)              
##   smoker (%)              414 ( 54.6)       553 ( 52.2)   <0.001     
##                           267 ( 35.2)       217 ( 20.5)              
##                            77 ( 10.2)       290 ( 27.4)              
##   sbp (mean (SD))      133.04 (21.31)    129.26 (19.85)   <0.001     
##   dbp (mean (SD))       80.80 (10.31)     79.88 (10.55)   <0.001     
##   histhtn (%)             233 ( 29.6)       379 ( 35.5)   <0.001     
##                           553 ( 70.4)       690 ( 64.5)              
##   txhtn (%)               659 ( 83.8)       804 ( 77.8)    0.002     
##                           127 ( 16.2)       230 ( 22.2)              
##   chol (mean (SD))     223.67 (44.37)    213.03 (45.92)   <0.001     
##   hdl (mean (SD))       52.34 (15.60)     53.24 (14.23)    0.208     
##   triglyc (mean (SD))  113.94 (70.69)    117.27 (76.01)    0.582     
##   ldl (mean (SD))      149.03 (38.61)    136.32 (39.68)   <0.001     
##   histchol (%)            256 ( 33.2)       356 ( 33.2)   <0.001     
##                           515 ( 66.8)       715 ( 66.8)              
##   txchol (%)              705 ( 91.2)       903 ( 87.2)   <0.001     
##                            68 (  8.8)       132 ( 12.8)              
##   height (mean (SD))   162.01 (9.39)     163.34 (9.05)     0.003     
##   weight (mean (SD))    73.84 (13.95)     73.60 (13.87)    0.150     
##   bmi (mean (SD))       28.10 (4.62)      27.56 (4.63)    <0.001     
##   phyact (mean (SD))   421.74 (377.13)   351.16 (378.05)  <0.001     
##   pcs (mean (SD))       49.01 (9.63)      50.14 (8.91)     0.032     
##   mcs (mean (SD))       48.90 (10.95)     46.87 (10.75)   <0.001     
##   cv (%)                  706 ( 95.3)       977 ( 95.4)    0.161     
##                            35 (  4.7)        47 (  4.6)              
##   tocv (mean (SD))    1685.62 (1079.72) 1793.38 (1071.81)  0.099     
##   death (%)               657 ( 89.0)       949 ( 92.8)   <0.001     
##                            81 ( 11.0)        74 (  7.2)              
##   todeath (mean (SD)) 1674.37 (1050.09) 1758.19 (1054.68)  0.252

comparegroups只需要1行:

descrTable(year ~ . - id, data = regicor)
## 
## --------Summary descriptives table by 'Recruitment year'---------
## 
## ______________________________________________________________________________________________ 
##                                                     1995        2000        2005     p.overall 
##                                                     N=431       N=786      N=1077              
## ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ 
## Age                                              54.1 (11.7) 54.3 (11.2) 55.3 (10.6)   0.078   
## Sex:                                                                                   0.506   
##     Male                                         206 (47.8%) 390 (49.6%) 505 (46.9%)           
##     Female                                       225 (52.2%) 396 (50.4%) 572 (53.1%)           
## Smoking status:                                                                       <0.001   
##     Never smoker                                 234 (56.4%) 414 (54.6%) 553 (52.2%)           
##     Current or former < 1y                       109 (26.3%) 267 (35.2%) 217 (20.5%)           
##     Former >= 1y                                 72 (17.3%)  77 (10.2%)  290 (27.4%)           
## Systolic blood pressure                          133 (19.2)  133 (21.3)  129 (19.8)   <0.001   
## Diastolic blood pressure                         77.0 (10.5) 80.8 (10.3) 79.9 (10.6)  <0.001   
## History of hypertension:                                                              <0.001   
##     Yes                                          111 (25.8%) 233 (29.6%) 379 (35.5%)           
##     No                                           320 (74.2%) 553 (70.4%) 690 (64.5%)           
## Hypertension treatment:                                                                0.002   
##     No                                           360 (83.5%) 659 (83.8%) 804 (77.8%)           
##     Yes                                          71 (16.5%)  127 (16.2%) 230 (22.2%)           
## Total cholesterol                                225 (43.1)  224 (44.4)  213 (45.9)   <0.001   
## HDL cholesterol                                  51.9 (14.5) 52.3 (15.6) 53.2 (14.2)   0.208   
## Triglycerides                                    114 (74.4)  114 (70.7)  117 (76.0)    0.582   
## LDL cholesterol                                  152 (38.4)  149 (38.6)  136 (39.7)   <0.001   
## History of hyperchol.:                                                                <0.001   
##     Yes                                          97 (22.5%)  256 (33.2%) 356 (33.2%)           
##     No                                           334 (77.5%) 515 (66.8%) 715 (66.8%)           
## Cholesterol treatment:                                                                <0.001   
##     No                                           403 (93.5%) 705 (91.2%) 903 (87.2%)           
##     Yes                                          28 (6.50%)  68 (8.80%)  132 (12.8%)           
## Height (cm)                                      163 (9.21)  162 (9.39)  163 (9.05)    0.003   
## Weight (Kg)                                      72.3 (12.6) 73.8 (14.0) 73.6 (13.9)   0.150   
## Body mass index                                  27.0 (4.15) 28.1 (4.62) 27.6 (4.63)  <0.001   
## Physical activity (Kcal/week)                     491 (419)   422 (377)   351 (378)   <0.001   
## Physical component                               49.3 (8.08) 49.0 (9.63) 50.1 (8.91)   0.032   
## Mental component                                 49.2 (11.3) 48.9 (11.0) 46.9 (10.8)  <0.001   
## Cardiovascular event:                                                                  0.161   
##     No                                           388 (97.5%) 706 (95.3%) 977 (95.4%)           
##     Yes                                          10 (2.51%)  35 (4.72%)  47 (4.59%)            
## Days to cardiovascular event or end of follow-up 1784 (1101) 1686 (1080) 1793 (1072)   0.099   
## Overall death:                                                                        <0.001   
##     No                                           369 (95.3%) 657 (89.0%) 949 (92.8%)           
##     Yes                                          18 (4.65%)  81 (11.0%)  74 (7.23%)            
## Days to overall death or end of follow-up        1713 (1042) 1674 (1050) 1758 (1055)   0.252   
## ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯

而且还能直接导出到Word,直接变成三线表!

如果是简单的做个三线表的话还是compareGroups更简单,但是tableone的功能貌似更多一点,比如除了这个三线表,它还可以用于加权数据中,还能用于倾向性评分中。