R语言与医学统计图形-【34】绘制统计表格

表的绘制,主要是临床三线表。

1.tableone包

#install.packages('tableone')
library(tableone)

set.seed(2017)
age <- sample(30:90,200,replace = T)
gender <- sample(c('Male','Female'),200,replace = T)
cholesterol <- rnorm(200,140,30)
BMI <- rnorm(200,27,8)
Smoking <- sample(c('Yes','No'),200,replace = T)
SBP <- rnorm(200,130,20)
education <- sample(c('High','Middle','Low'),200,replace = T)
income <- sample(c('High','Middle','Low'),200,replace = T)
dt <- data.frame(age=age,gender=gender,
                 cho=round(cholesterol,2),
                 bmi=round(BMI,2),
                 smoking=Smoking,
                 sbp=round(SBP,2),
                 edu=education,
                 income=income)

#将变量名传入变量列表中
vars <- names(dt)
#定义分类变量,用于表格中数据分层
catvars <- c('gender','smoking','edu','income')
table1 <- CreateTableOne(vars = vars, #定义变量列表
                        data = dt, #数据
                        factorVars = catvars) #指定分类变量
table1


#分层,增加t检验(连续变量)和卡方检验(分类变量)结果
vars2 <- c('age','cho','sbp','bmi','smoking','edu','income')
table2 <- CreateTableOne(vars2,dt,catvars,strata = c('gender'))
table2

table1结果:


image.png

table2结果:


image.png

快速导出tableone产出的表格。

if(! require("rJava")) install.packages("rJava")
if(require('rJava')){
 # https://cran.r-project.org/src/contrib/Archive/ReporteRs/
  if(! require("ReporteRs")) install.packages("ReporteRs")
  devtools::install_github('davidgohel/ReporteRsjars')
  devtools::install_github('davidgohel/ReporteRs')
}

ReporteRs包安装失败。

2.table1包

if(!require(table1)) install.packages("table1",ask=F,update=F)
require(table1)

library(boot) #使用自带数据
melanoma2 <- melanoma
head(melanoma)
dim(melanoma)

#将感兴趣的因子作为分类
melanoma2$status <- factor(melanoma2$status,
                           levels = c(2,1,3),
                           labels = c('Alive',
                                      'Melanoma death',
                                      'Non-melanoma death'))

#格式: ~感兴趣的基线变量|感兴趣的分类变量,data
table1(~ factor(sex)+age+factor(ulcer)+thickness|status,
       data = melanoma2)
image.png

修饰表格。

## 给分类变量sex指定标签
melanoma2$sex <- 
  factor(melanoma2$sex, levels=c(1,0),
         labels=c("Male", 
                  "Female"))
## 给分类变量ulcer指定标签
melanoma2$ulcer <- 
  factor(melanoma2$ulcer, levels=c(0,1),
         labels=c("Absent", 
                  "Present"))
## 给变量名指定标签
label(melanoma2$sex)       <- "Sex"
label(melanoma2$age)       <- "Age"
label(melanoma2$ulcer)     <- "Ulceration"
label(melanoma2$thickness) <- "Thickness"

## 给连续型变量指定单位
units(melanoma2$age)       <- "years"
units(melanoma2$thickness) <- "mm"

## 再增加overall统计量
table1(~ sex + age + ulcer + thickness | status, data=melanoma2, overall="Total")
image.png

进一步细节修饰。

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)
image.png

这个包更多使用参考:临床三线表

你可能感兴趣的:(R语言与医学统计图形-【34】绘制统计表格)