tableone用到的示例数据库和默认参数详细的解释在tableone详细解析(1)------分类变量-CSDN博客里有介绍,本章的讲解尽量简洁。
——————————————————我是一个倔强的分割线———————————————
先用lapply()把分类变量转换成factor
library(tableone)
library(survival)#示例数据需要加载的包
data(pbc)#示例数据
head(pbc)#查看示例数据的简要信息
## 把categorical variables 变成 factors
#lapply()函数负责把变量属性变成factor
varsToFactor <- c("status","trt","ascites","hepato","spiders","edema","stage")
pbc[varsToFactor] <- lapply(pbc[varsToFactor], factor)
dput(names(数据框))这个函数可太棒了,直接粘贴变量
新建tableone
#新建一个tableone的变量列表,以trt分组
dput(names(pbc))
vars <- c("time","status","age","sex","ascites","hepato",
"spiders","edema","bili","chol","albumin",
"copper","alk.phos","ast","trig","platelet",
"protime","stage")
tableOne <- CreateTableOne(vars = vars, strata = c("trt"), data = pbc)
查看tableone的信息,可以进行输出内容的设置
以下举例对哪些变量进行exact test:exact=c(""),对哪些变量进行二分类变量全部信息展示 :cramVars="",展示全部SMD:smd=TRUE。
##非正态变量nonnormal = c("bili","chol","copper","alk.phos","trig")
##cramVars = "hepato"#只对"hepato"这个二分类变量展示全部分层频率(百分比)
##exact = c("status","stage")#对"status","stage"运行exact test
print(tableOne, nonnormal = c("bili","chol","copper","alk.phos","trig"),
exact = c("status","stage"), cramVars = "hepato", smd = TRUE,quote = TRUE, noSpaces = TRUE)
单独查看分类变量或连续性变量也可以
## 可以只看tableone中的分类变量信息
tableOne$CatTable
summary(tableOne$CatTable)
## 可以只看tableone中的连续性变量信息
tableOne$ContTable
summary(tableOne$ContTable)