如何使用tableone按行更改表格百分比?

问题描述 投票:0回答:2

我使用

library(tableone)
对多个变量进行描述性统计 这是我的代码:

library(tableone)

myVars <- c("class", "age", "Sex", "bmi", "bmi_category",
            "drink_freq", "smoke_yn", "edu_dummy")

catVars <- c("class", "Sex", "bmi_category",
             "drink_freq", "smoke_yn", "edu_dummy")

tab1_inf <- CreateTableOne(vars = myVars, strata = "NEWDI",
                             data = TKA_table1, factorVars = catVars)

a1 <- print(tab1_inf, exact = "NEWDI", showAllLevels = TRUE)

默认为百分比,我想更改它的格式,如下所示(示例):

我检查了它的描述,发现没有可以设置的选项。 https://rdrr.io/cran/tableone/man/print.TableOne.html 我该怎么办?

r row percentage
2个回答
1
投票

通过一些巧妙的动手操作,您可以操纵 TableOne 对象中的百分比。这使用来自

pbc
包的名为
survival
的示例数据集。

library(tableone)
library(survival)
data(pbc)

## Make categorical variables factors
varsToFactor <- c("status","trt","ascites","hepato","spiders","edema","stage")
pbc[varsToFactor] <- lapply(pbc[varsToFactor], factor)

## Create a variable list
vars <- c("time","status","age","sex","ascites","hepato",
          "spiders","edema","bili","chol","albumin",
          "copper","alk.phos","ast","trig","platelet",
          "protime","stage")

## Create Table 1 stratified by trt
tableOne <- CreateTableOne(vars = vars, strata = c("trt"), data = pbc)

tableOne

之前

                      Stratified by trt
                       1                 2                 p      test
  n                        158               154                      
  time (mean (SD))     2015.62 (1094.12) 1996.86 (1155.93)  0.883     
  status (%)                                                0.894     
     0                      83 (52.5)         85 (55.2)               
     1                      10 ( 6.3)          9 ( 5.8)               
     2                      65 (41.1)         60 (39.0)               
  age (mean (SD))        51.42 (11.01)     48.58 (9.96)     0.018     
  sex = f (%)              137 (86.7)        139 (90.3)     0.421     
  ascites = 1 (%)           14 ( 8.9)         10 ( 6.5)     0.567     
  hepato = 1 (%)            73 (46.2)         87 (56.5)     0.088     
  spiders = 1 (%)           45 (28.5)         45 (29.2)     0.985 
...

您应该尝试根据您自己的数据格式调整以下代码:

for (i in 1:length(table1)) {
    sum = tableOne$CatTable[[1]][[i]]$freq + tableOne$CatTable[[2]][[i]]$freq
    tableOne$CatTable[[1]][[i]]$percent = tableOne$CatTable[[1]][[i]]$freq / sum
    tableOne$CatTable[[2]][[i]]$percent = tableOne$CatTable[[2]][[i]]$freq / sum
  }
}

tableOne

之后

                      Stratified by trt
                       1                 2                 p      test
  n                        158               154                      
  time (mean (SD))     2015.62 (1094.12) 1996.86 (1155.93)  0.883     
  status (%)                                                0.894     
     0                      83 (0.5)          85 (0.5)                
     1                      10 (0.5)           9 (0.5)                
     2                      65 (0.5)          60 (0.5)                
  age (mean (SD))        51.42 (11.01)     48.58 (9.96)     0.018     
  sex = f (%)              137 (0.5)         139 (0.5)      0.421     
  ascites = 1 (%)           14 (0.6)          10 (0.4)      0.567     
  hepato = 1 (%)            73 (0.5)          87 (0.5)      0.088     
  spiders = 1 (%)           45 (0.5)          45 (0.5)      0.985   

-1
投票

你是如何解决这个问题的?上面的解决方案对我不起作用,而且似乎对 Vons 也不起作用(全部填充到 0.5)?我可能会遗漏一些东西,但是......?

我还没有50声望,所以我无法发表评论,但这应该是一个评论。

© www.soinside.com 2019 - 2024. All rights reserved.