如何在 tableone 包中的 createtableone 命令中按行报告百分比?
How to report percentages by rows in createtableone command in tableone package?
我想使用 r 中 tableone 包中的 CreateTableOne 按行报告百分比,使用以下代码
vars<-c( "Group1vsGroup2", "Single.Institution",
"Internal.Funding", "National.Funding",
"Industr1.Funding")
CatVar<-c( "Single.Institution",
"Internal.Funding", "National.Funding",
"Industr1.Funding")
tab2 <- CreateTableOne(vars = vars, strata = "Group1vsGroup2",factorVars = CatVar,data = df,test = T);tab2<-print(tab2, margin=1,test = T, varLabels = T,quote = T,dropEqual = T)
我添加了 "margin=1",正如我在这个 website 中看到的,虽然它是针对 tableone 命令(而不是 CreateTableOne),但我得到了如下图所示的按列的百分比。任何建议将不胜感激。
这里是您可以开始的 MWE,请参阅 ?Gmisc::getDescriptionStatsBy
了解更多详细信息。
library(Gmisc)
# Define cyl as a factor so that getDescriptionStatsBy can use the correct Gmisc::describe*
mtcars$cyl=as.factor(mtcars$cyl)
getTable1Stats <- function(x, digits = 0,...){
getDescriptionStatsBy(x = x,
by = mtcars$am,
digits = digits,
header_count = TRUE,
...)
}
t1 <- list()
t1[["Gas"]] <- getTable1Stats(mtcars$mpg, add_total_col="last")
#hrzl_prop=FALSE will indicate that the proportions are to be interpreted in a vertical manner
t1[["Cylinder†"]] <- getTable1Stats(mtcars$cyl, hrzl_prop=TRUE, add_total_col="last")
t1[["Disp"]] <- getTable1Stats(mtcars$disp, continuous_fn=describeMedian, add_total_col="last")
mergeDesc(t1,
htmlTable_args = list(css.rgroup = "",
caption = "Basic descriptive statistics from the mtcars dataset",
tfoot = "† The weight is in 10<sup>3</sup> kg")
)
PS:此解决方案基于 Max 的插图 here. For more details on htmlTable
you can see its vignette here
我想使用 r 中 tableone 包中的 CreateTableOne 按行报告百分比,使用以下代码
vars<-c( "Group1vsGroup2", "Single.Institution",
"Internal.Funding", "National.Funding",
"Industr1.Funding")
CatVar<-c( "Single.Institution",
"Internal.Funding", "National.Funding",
"Industr1.Funding")
tab2 <- CreateTableOne(vars = vars, strata = "Group1vsGroup2",factorVars = CatVar,data = df,test = T);tab2<-print(tab2, margin=1,test = T, varLabels = T,quote = T,dropEqual = T)
我添加了 "margin=1",正如我在这个 website 中看到的,虽然它是针对 tableone 命令(而不是 CreateTableOne),但我得到了如下图所示的按列的百分比。任何建议将不胜感激。
这里是您可以开始的 MWE,请参阅 ?Gmisc::getDescriptionStatsBy
了解更多详细信息。
library(Gmisc)
# Define cyl as a factor so that getDescriptionStatsBy can use the correct Gmisc::describe*
mtcars$cyl=as.factor(mtcars$cyl)
getTable1Stats <- function(x, digits = 0,...){
getDescriptionStatsBy(x = x,
by = mtcars$am,
digits = digits,
header_count = TRUE,
...)
}
t1 <- list()
t1[["Gas"]] <- getTable1Stats(mtcars$mpg, add_total_col="last")
#hrzl_prop=FALSE will indicate that the proportions are to be interpreted in a vertical manner
t1[["Cylinder†"]] <- getTable1Stats(mtcars$cyl, hrzl_prop=TRUE, add_total_col="last")
t1[["Disp"]] <- getTable1Stats(mtcars$disp, continuous_fn=describeMedian, add_total_col="last")
mergeDesc(t1,
htmlTable_args = list(css.rgroup = "",
caption = "Basic descriptive statistics from the mtcars dataset",
tfoot = "† The weight is in 10<sup>3</sup> kg")
)
PS:此解决方案基于 Max 的插图 here. For more details on htmlTable
you can see its vignette here