R 表格速成
R數(shù)據(jù)處理能力非常強(qiáng)大,而且輸出也非常靈活。當(dāng)然在R里面的輸出都是print字符形式,我們?cè)赗里面看到結(jié)果很一目了然,但是往往需要把這些結(jié)果放在三線(xiàn)表里面。
在臨床研究中,我們經(jīng)常要用到三線(xiàn)表來(lái)展示數(shù)據(jù)與統(tǒng)計(jì)值。R可以輸出統(tǒng)計(jì)參數(shù)及檢驗(yàn)參數(shù),一個(gè)個(gè)往上粘貼很困難。
今天就來(lái)介紹一個(gè)簡(jiǎn)單有效的數(shù)據(jù)整理成表格的包。直接上效果圖

數(shù)據(jù)
這里使用survival自帶的數(shù)據(jù),pbc,也感謝給了詳細(xì)介紹。 我們使用pbc臨床數(shù)據(jù)作為案例,來(lái)如何使用tableone;為方便展示,這里只選擇了7個(gè)變量。
首先我們將數(shù)據(jù)變成tibble格式,然后轉(zhuǎn)換trt與status為因子。
最后skim一下,看數(shù)據(jù)的缺失與分布狀況。skim有一個(gè)好處就是,可以對(duì)數(shù)據(jù)進(jìn)行整個(gè)的概況輸出,哪些為因子類(lèi)factor,哪些為連續(xù)性變量numeric。以及n_missing缺失值。hist可以提示這個(gè)變量的分布類(lèi)型,偏態(tài)分布還是正態(tài)分布。
library(survival)
library(tableone)
library(skimr)
## Load data
data(pbc)
pbc=pbc %>% as.tbl() %>%
mutate(trt=factor(trt,labels = c("yes","no")),
status=factor(status, labels =c("status","edema","stage")))
df=pbc %>% as.tbl() %>%
mutate(trt=factor(trt,labels = c("yes","no")),
status=factor(status, labels =c("status","edema","stage"))) %>%
select(time,age,sex,status,trt,bili,platelet)
df
> df
# A tibble: 418 x 7
time age sex status trt bili platelet
<int> <dbl> <fct> <fct> <fct> <dbl> <int>
1 400 58.8 f stage yes 14.5 190
2 4500 56.4 f status yes 1.1 221
3 1012 70.1 m stage yes 1.4 151
4 1925 54.7 f stage yes 1.8 183
5 1504 38.1 f edema no 3.4 136
6 2503 66.3 f stage no 0.8 NA
7 1832 55.5 f status no 1 204
8 2466 53.1 f stage no 0.3 373
9 2400 42.5 f stage yes 3.2 251
10 51 70.6 f stage no 12.6 302
# … with 408 more rows
> skim(df)
── Data Summary ────────────────────────
Values
Name df
Number of rows 418
Number of columns 7
_______________________
Column type frequency:
factor 3
numeric 4
________________________
Group variables None
── Variable type: factor ──────────────────────────────────────────────────────────────────────────────
skim_variable n_missing complete_rate ordered n_unique top_counts
1 sex 0 1 FALSE 2 f: 374, m: 44
2 status 0 1 FALSE 3 sta: 232, sta: 161, ede: 25
3 trt 106 0.746 FALSE 2 yes: 158, no: 154
── Variable type: numeric ─────────────────────────────────────────────────────────────────────────────
skim_variable n_missing complete_rate mean sd p0 p25 p50 p75 p100 hist
1 time 0 1 1918. 1105. 41 1093. 1730 2614. 4795 ▅▇▆▃▂
2 age 0 1 50.7 10.4 26.3 42.8 51.0 58.2 78.4 ▂▆▇▅▁
3 bili 0 1 3.22 4.41 0.3 0.8 1.4 3.4 28 ▇▁▁▁▁
4 platelet 11 0.974 257. 98.3 62 188. 251 318 721 ▅▇▃▁▁
基線(xiàn)數(shù)據(jù)
我們來(lái)看一下,tableone匯總的結(jié)果,這個(gè)比skim簡(jiǎn)潔,是我們臨床上想要的基線(xiàn)資料,(mean (SD)) 及(%) 。而且也不需要指定哪些為分類(lèi)變量,哪些為連續(xù)性變量,因?yàn)樵谝婚_(kāi)始我們就mutate了數(shù)據(jù)類(lèi)型。
但是我們發(fā)現(xiàn):
沒(méi)有中位數(shù)
分類(lèi)變量只顯示F
后面會(huì)說(shuō),如何解決這類(lèi)問(wèn)題。
> CreateTableOne(data = df)
Overall
n 418
time (mean (SD)) 1917.78 (1104.67)
age (mean (SD)) 50.74 (10.45)
sex = f (%) 374 (89.5)
status (%)
status 232 (55.5)
edema 25 ( 6.0)
stage 161 (38.5)
trt = no (%) 154 (49.4)
bili (mean (SD)) 3.22 (4.41)
platelet (mean (SD)) 257.02 (98.33)
分組數(shù)據(jù)
上面是匯總的數(shù)據(jù),那么我們需要按組來(lái)分組展示,譬如以trt為group。
看到這個(gè)輸出,我們覺(jué)得,嗯,這就是我們要的結(jié)果,后面也給出了具體的p值。
但是沒(méi)有統(tǒng)計(jì)學(xué)檢驗(yàn)值。
T1=CreateTableOne(data=df,strata = c("trt"))
T1
Stratified by trt
yes no 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
sex = f (%) 137 (86.7) 139 ( 90.3) 0.421
status (%) 0.894
status 83 (52.5) 85 ( 55.2)
edema 10 ( 6.3) 9 ( 5.8)
stage 65 (41.1) 60 ( 39.0)
trt = no (%) 0 ( 0.0) 154 (100.0) <0.001
bili (mean (SD)) 2.87 (3.63) 3.65 (5.28) 0.131
platelet (mean (SD)) 258.75 (100.32) 265.20 (90.73) 0.555
或者我們print一下,其實(shí)很多參數(shù)都是在print中設(shè)置,詳情見(jiàn)微調(diào)基線(xiàn)特征表1輸出格式。
只需要添加showAllLevels = TRUE即可顯示factor的全部level。
或者可以再顯示全部的total
# add level
print(T1,showAllLevels = TRUE)
Stratified by trt
level yes no 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
sex (%) m 21 ( 13.3) 15 ( 9.7) 0.421
f 137 ( 86.7) 139 ( 90.3)
status (%) status 83 ( 52.5) 85 ( 55.2) 0.894
edema 10 ( 6.3) 9 ( 5.8)
stage 65 ( 41.1) 60 ( 39.0)
trt (%) yes 158 (100.0) 0 ( 0.0) <0.001
no 0 ( 0.0) 154 (100.0)
bili (mean (SD)) 2.87 (3.63) 3.65 (5.28) 0.131
platelet (mean (SD)) 258.75 (100.32) 265.20 (90.73) 0.555
# add total
T2=CreateTableOne(data=df,strata = c("trt"),addOverall = T)
T2
Stratified by trt
Overall yes no p test
n 418 158 154
time (mean (SD)) 1917.78 (1104.67) 2015.62 (1094.12) 1996.86 (1155.93) 0.883
age (mean (SD)) 50.74 (10.45) 51.42 (11.01) 48.58 (9.96) 0.018
sex = f (%) 374 (89.5) 137 (86.7) 139 ( 90.3) 0.421
status (%) 0.894
status 232 (55.5) 83 (52.5) 85 ( 55.2)
edema 25 ( 6.0) 10 ( 6.3) 9 ( 5.8)
stage 161 (38.5) 65 (41.1) 60 ( 39.0)
trt = no (%) 154 (49.4) 0 ( 0.0) 154 (100.0) <0.001
bili (mean (SD)) 3.22 (4.41) 2.87 (3.63) 3.65 (5.28) 0.131
platelet (mean (SD)) 257.02 (98.33) 258.75 (100.32) 265.20 (90.73) 0.555
檢驗(yàn)方法切換
有時(shí)候,并不是所有數(shù)據(jù)都是服從正態(tài)分布,頻數(shù)分布有時(shí)候也需要用到Fisher 檢驗(yàn),這時(shí)候需要我們自定義了。
這里在Print需要用nonnormal來(lái)指定哪些變量為非正態(tài)分布及exact 來(lái)指定確切概率法檢驗(yàn)。
# with nonnormal and exact
T3=print(T1,showAllLevels = TRUE,nonnormal = c("bili","platelet"),
exact = "sex")
Stratified by trt
level yes no 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
sex (%) m 21 ( 13.3) 15 ( 9.7) 0.377 exact
f 137 ( 86.7) 139 ( 90.3)
status (%) status 83 ( 52.5) 85 ( 55.2) 0.894
edema 10 ( 6.3) 9 ( 5.8)
stage 65 ( 41.1) 60 ( 39.0)
trt (%) yes 158 (100.0) 0 ( 0.0) <0.001
no 0 ( 0.0) 154 (100.0)
bili (median [IQR]) 1.40 [0.80, 3.20] 1.30 [0.72, 3.60] 0.842 nonnorm
platelet (median [IQR]) 255.00 [189.50, 322.00] 259.50 [206.75, 322.50] 0.455 nonnorm
Markdown輸出
我們上述的結(jié)果都在R consle里面,但是最后還是要回歸到三線(xiàn)表,
那么如何輸出呢?這里給出了答案。R語(yǔ)言統(tǒng)計(jì)與繪圖:快速繪制臨床基線(xiàn)特征表1
簡(jiǎn)單粗暴的方法:就是復(fù)制粘貼,使用quote = TRUE顯示引號(hào),使用noSpaces = TRUE刪除用于在R控制臺(tái)中對(duì)齊文本的空格,然后直接復(fù)制基線(xiàn)表整個(gè)內(nèi)容并將其粘貼到Excel電子表格即可。
write.csv(T3, file = "Table.csv")
同樣在Rmarkdown中,最后生成報(bào)告,也需要一個(gè)美觀的Table,只需要
knitr::kable(T3)
DT::datatable(T3)
