install.packages("tableone")
install.packages("survival")
library(tableone)
library(survival)
str(pbc)
#以pbc数据集为例
varsToFactor
vars
"spiders","edema","bili","chol","albumin",
"copper","alk.phos","ast","trig","platelet",
"protime","stage")
tableOne
#以“trt作为分层变量”,factorVars 定义因子型变量
print(tableOne,showAllLevels = TRUE)
#showAllLevels = TRUE显示所有水平的频数和百分比
#默认对定量变量进行t检验或方差分析,对分类变量进行卡方检验,并输出P值
tableOne$CatTable
#仅输出分类变量结果
tableOne$ContTable
#仅输出定量变量结果
summary(tableOne$CatTable)
#分类变量统计描述详细信息
summary(tableOne$ContTable)
#定量变量统计描述详细信息
print(tableOne,nonnormal= c("bili","chol","copper","alk.phos","trig"),
exact= c("status","stage"), showAllLevels = TRUE )
#nonnormal指定非正态变量,对其进行wilcox.test或 kruskal.test;exact指定进行fisher.test的变量
print(tableOne,nonnormal= c("bili","chol","copper","alk.phos","trig"),
exact= c("status","stage"), showAllLevels = TRUE,quote = TRUE)
#将输入结果复制到txt文件中
#打开Excel,依次点击“数据”菜单——“自文本”——“分隔符号”——“空格”——“确定”,即可将结果输入到excel表格中。
领取专属 10元无门槛券
私享最新 技术干货