gpt4 book ai didi

r - 如何在 tableone 包中的 createtableone 命令中按行报告百分比?

转载 作者:行者123 更新时间:2023-12-04 13:18:05 27 4
gpt4 key购买 nike

我想使用 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)

我在这个 website 中看到添加了“margin=1”虽然它是针对 tableone 命令(不是 CreateTableOne),但我得到了 % 按列显示,如下图所示。任何建议将不胜感激。 enter image description here

最佳答案

您可以从这里开始使用 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&dagger;"]] <- 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 = "&dagger; The weight is in 10<sup>3</sup> kg")
)

enter image description here

PS:此解决方案基于 Max 的插图 here .有关 htmlTable 的更多详细信息,您可以查看其小插图 here

关于r - 如何在 tableone 包中的 createtableone 命令中按行报告百分比?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57739156/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com