I didn't want to write this tutorial anymore, because I have written a lot before, and I also mentioned the R package TableOne. But this R package is really amazing. You can easily make Table 1 (baseline data table) and Table 2 (single factor analysis table) of SCI papers, and can easily make Table 3 of the comparison of multiple models in sci papers ( Multi-factor analysis table), you can also make a linear trend (P for trend). It is no exaggeration to say that using this R package, you can write a simple SCI paper. The following table can be passed through the compareGroups package It's easy to make.
This kind of watch can also be made, and it is not difficult
. Don't talk nonsense, start work right away.
In addition to the compareGroups package, we also need to import a glue package, otherwise an error will be reported.
We still use the previous breast cancer data, first import the R package and view the data
library(foreign)
library(compareGroups)
library(glue)
bc <- read.spss("E:/r/test/Breast cancer survival agec.sav",
use.value.labels=F, to.data.frame=T)
bc <- na.omit(bc)
head(bc)
Convert categorical variables into factors
####转换为因子
bc$age1 <- factor(bc$age1)
bc$lnpos <- factor(bc$lnpos)
bc$histgrad <- factor(bc$histgrad)
bc$er<- factor(bc$er)
bc$pr <- factor(bc$pr)
bc$status<- factor(bc$status)
bc$pathscat<- factor(bc$pathscat)
bc$ln_yesno<- factor(bc$ln_yesno)
First look at the distribution of variables in the overall population
descrTable( ~ ., data = bc)
Grouped by case histology, denoted by histgrad
descrTable( histgrad~ ., data = bc)####.符号代表包括其他的变量
It seems not bad, go on. If you don’t want the variable of agec
descrTable( histgrad~age+pathsize+er+status+pathscat+ln_yesno+ time, data = bc)
If a variable such as pathsize is non-normally distributed, it can be specified
descrTable( histgrad~age+pathsize+er+status+pathscat+ln_yesno+ time, data = bc,method = c
(pathsize=2))##用中位数表示
It can also automatically check whether the distribution is normal
descrTable( histgrad~age+pathsize+er+status+pathscat+ln_yesno+ time,
data = bc,method = c(pathsize=NA))##自动检验是否正态分布
The OR or HR value can be more important when grouping as a binary variable. Now it is grouped by whether the lymph nodes are enlarged or not.
descrTable( ln_yesno~age+pathsize+er+status+pathscat+histgrad+ time,
data = bc, ref= 1,show.ratio=T)##参考水平为1
You can also change the reference level of a group
descrTable( ln_yesno~age+pathsize+er+status+pathscat+histgrad+ time,
data = bc, ref= c(pathscat=2),show.ratio=T)##更改参考水平为
Change the continuous variable for each P-SD, add I want to see the change for every 10 years of age increase
descrTable( ln_yesno~age+pathsize+er+status+pathscat+histgrad+ time,
data = bc, ref= 1,show.ratio=T,fact.ratio = 10)##参考水平为1,更改连续变量每个P-SD
Adjust the display digits of the decimal point
descrTable( ln_yesno~age+pathsize+er+status+pathscat+histgrad+ time,
data = bc, ref= 1,show.ratio=T,digits = 3)##参考水平为1,调整小数点显示位数
Finally, there is a big trick to add stratification variables
tab<-descrTable( ln_yesno~age+pathsize+status+pathscat+histgrad+ time,
data = bc)
be<-strataTable(tab, "er")
be
export data
export2csv(tab,file = "tab.csv")
export2word(tab, file='table1.docx')
As shown in the figure,
it can be published with a slight modification.
References:
- compareGroups package instruction manual
- https://mp.weixin.qq.com/s/WDr9mwsv8–NPLEBjtRiZA For
more exciting articles, please pay attention to the public account: zero-based scientific research