本文介绍了如何在 tableone 包中的 createtableone 命令中按行报告百分比?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用以下代码使用 r 中 tableone 包中的 CreateTableOne 按行报告百分比

I want to report percentage by rows using CreateTableOne from tableone package in r using the following code

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)

我在这个

推荐答案

这里有一个 MWE,您可以从这里开始,请参阅 ?Gmisc::getDescriptionStatsBy 了解更多详细信息.

Here a MWE you can start with, see ?Gmisc::getDescriptionStatsBy for more details.

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")
)

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

PS: This solution based on Max's vignette here. For more details on htmlTable you can see its vignette here

这篇关于如何在 tableone 包中的 createtableone 命令中按行报告百分比?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 20:52