table1

代码主要来自table1的帮助文档

1.输入数据

library(table1)
dat <- expand.grid(id=1:10, sex=c("Male", "Female"), treat=c("Treated", "Placebo"))
dat$age <- runif(nrow(dat), 10, 50)
dat$age[3] <- NA  # Add a missing value
dat$wt <- exp(rnorm(nrow(dat), log(70), 0.2))

# 每一列的标签,会出现在表格里
label(dat$sex) <- "Sex"
label(dat$age) <- "Age"
label(dat$treat) <- "Treatment Group"
label(dat$wt) <- "Weight"

# 单位,会出现在表格里
units(dat$age) <- "years"
units(dat$wt) <- "kg"

这个就是输入数据的格式了

head(dat)
##   id  sex   treat      age       wt
## 1  1 Male Treated 19.19113 54.57930
## 2  2 Male Treated 27.94805 40.97775
## 3  3 Male Treated       NA 72.69000
## 4  4 Male Treated 40.89000 76.16845
## 5  5 Male Treated 24.67689 63.15861
## 6  6 Male Treated 32.16987 63.71638

2.生成表格

table(dat$treat)
## 
## Treated Placebo 
##      20      20
# 根据treat列分组展示
table1(~ sex + age + wt | treat, data=dat)
# 不要overall列
table1(~ sex + age + wt | treat, data=dat,overall = F)
# 根据treat和sex两列分组展示
table1(~ age + wt | treat*sex, data=dat)
# 不分组
table1(~ treat + sex + age + wt, data=dat)

3.换数据

ACC 的临床信息

https://gdc.cancer.gov/about-data/publications/acc_2016

cl = read.delim("ACC.CLINICAL.asof20140923.txt")
#配上免疫亚型和分子亚型数据
load("TCGA_pancancer_ph.Rdata")
cl = merge(cl,ph,by.x = "bcr_patient_barcode",
by.y = "X_PATIENT")
cl = cl[,c("age_at_initial_pathologic_diagnosis",
           "pathologic_stage",
           "gender.x",
           "Subtype_mRNA",
           "Subtype_Immune_Model_Based")]
colnames(cl) = c("age","stage","gender","subtype_mrna","immu")
library(stringr)
cl$gender = str_to_title(cl$gender)
cl$subtype = ifelse(str_detect(cl$subtype_mrna,"high"),"HSP","LSP")
cl$Proliferation = ifelse(str_detect(cl$subtype_mrna,"proliferation"),"High","Low")

label(cl$gender) <- "Gender"
label(cl$age) <- "Age"
label(cl$immu) <- "Immune Subtype"
label(cl$stage) <- "Stage"

table1(~ age + stage + gender +Proliferation + immu |subtype, data=cl,overall = F)

你可能感兴趣的:(table1)