三线表c
require(table1)
require(tableone)
library(boot)
melanoma2 <- melanoma
head(melanoma)
dim(melanoma)
melanoma2status,
levels=c(2,1,3),
labels=c("Alive",
"Melanoma death",
"Non-melanoma death")) ##更改数据框中的数据
CreateTableOne(data = melanoma2)
table1(~ factor(sex) + age + factor(ulcer) + thickness | status, data=melanoma2)
给分类变量sex指定标签
melanoma2sex, levels=c(1,0),
labels=c("Male",
"Female"))
给分类变量ulcer指定标签
melanoma2ulcer, levels=c(0,1),
labels=c("Absent",
"Present"))
给变量名指定标签
label(melanoma2age) <- "Age"
label(melanoma2thickness) <- "Thickness"
给连续型变量指定单位
units(melanoma2thickness) <- "mm"
再增加overall统计量
table1(~ sex + age + ulcer + thickness | status, data=melanoma2, overall="Total")
细节控制
labels <- list(
variables=list(sex="Sex",
age="Age (years)",
ulcer="Ulceration",
thickness="Thickness (mm)"),
groups=list("", "", "Death"))##表格上的第一级Death
重新给status命名标签,death放到上面去
levels(melanoma2$status) <- c("Alive", "Melanoma", "Non-melanoma")
按想要的顺序顺序设置分组或列,
Total放第一列,split分开status
strata <- c(list(Total=melanoma2), split(melanoma2, melanoma2$status))
添加渲染风格-连续型变量与分类变量展示不同
连续型渲染风格函数
my.render.cont <- function(x) {
with(stats.apply.rounding(stats.default(x), digits=2), c("",
"Mean (SD)"=sprintf("%s (± %s)", MEAN, SD)))
}
分类变量渲染风格
my.render.cat <- function(x) {
c("", sapply(stats.default(x), function(y) with(y,
sprintf("%d (%0.0f %%)", FREQ, PCT))))
}
结果
groupsapn为分组的个数,1为Total, 1为Alive,以及2为Death
增加了Death的亚组
table1(strata, labels, groupspan=c(1, 1, 2),
render.continuous=my.render.cont, render.categorical=my.render.cat)