三、基线表分析
安装包
install.packages("tableone")
加载包 tableone是用来做基线检验的安装包
library(tableone)
将分类变量转换为因子变量
varsToFactor <- c("status", "trt", "ascites", "hepato", "spiders", "edema", "stage")
data[varsToFactor] <- lapply(data[varsToFactor], factor)
vars <- c("time", "status", "age", "sex", "ascites", "hepato", "spiders", "edema", "bili", "chol", "albumin", "copper", "alk.phos", "ast", "trig", "platelet", "protime", "stage")
根据治疗组(trt)创建Table 1
tableOne <- CreateTableOne(vars = vars, strata = c("trt"), data = data)
只需输入对象名称,将调用print.TableOne方法
tableOne
setwd("E:\\神州医疗工作中心\\2024年工作")
read.csv(tableOne ,"result.csv") #保存在自己的存储路径中
另外一种保存方式
如果您的工作流程包括在撰写稿件时复制到Excel和Word中,您可能会受益于quote参数。这将引用所有内容,以免Excel搞乱了单元格。
方法1
tab1Mat <- print(tableOne, nonnormal = c("bili", "chol", "copper", "alk.phos", "trig"),
exact = c("status", "stage"), quote = TRUE)
write.csv(tab1Mat, file = "Result1_Table1.csv")
方法2
print(tableOne, formatOptions = list(big.mark = ","))
tab1Mat2 <- print(tableOne, showAllLevels = TRUE, quote = FALSE, noSpaces = TRUE, printToggle = FALSE)
write.csv(tab1Mat2, file = "Result2_Table1.csv")
print(tableOne, nonnormal = c("bili", "chol", "copper", "alk.phos", "trig"),
exact = c("status", "stage"), quote = TRUE, noSpaces = TRUE)