How to report percentages by rows in createtableone command in tableone package?

落爺英雄遲暮 提交于 2019-12-13 17:44:48

问题


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)

I added "margin=1" as I saw in this website although it was for tableone command (not CreateTableOne), but I got % by columns as in the image below. Any advice will be greatly appreciated.


回答1:


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: This solution based on Max's vignette here. For more details on htmlTable you can see its vignette here



来源:https://stackoverflow.com/questions/57739156/how-to-report-percentages-by-rows-in-createtableone-command-in-tableone-package

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!