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

huangapple go评论71阅读模式
英文:

How to use tableone to change table percentage by row?

问题

I am use library(tableone) to make my descriptive statistics for multiple variables
This is my code:

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

This it default for percentage, and I want change it format like this(example):
这是默认的百分比格式,我想要更改成像这样(示例):

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

I checked its description and found no options to set.
我查看了它的描述,没有找到设置选项。

https://rdrr.io/cran/tableone/man/print.TableOne.html
如何做到这一点?

英文:

I am use library(tableone) to make my descriptive statistics for multiple variables
This is my code:

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)

This it default for percentage, and I want change it format like this(example):
如何使用tableone按行更改表格百分比?

I checked its description and found no options to set.
https://rdrr.io/cran/tableone/man/print.TableOne.html
How can I do it?

答案1

得分: 1

通过一些巧妙的操作,您可以操纵TableOne对象中的百分比。这使用了一个名为pbc的示例数据集,来自于survival包。

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

## 将分类变量转换为因子
varsToFactor <- c("status","trt","ascites","hepato","spiders","edema","stage")
pbc[varsToFactor] <- lapply(pbc[varsToFactor], factor)

## 创建变量列表
vars <- c("time","status","age","sex","ascites","hepato",
          "spiders","edema","bili","chol","albumin",
          "copper","alk.phos","ast","trig","platelet",
          "protime","stage")

## 创建按trt分层的Table 1
tableOne <- CreateTableOne(vars = vars, strata = c("trt"), data = pbc)

tableOne

在之前的代码中,您可以尝试将以下代码适应您自己的数据格式:

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

在之后的TableOne对象中,您将看到百分比已被调整。

英文:

With some clever getting-your-hands dirty, you can manipulate the percentages in the TableOne object. This uses an example dataset called pbc from survival package.

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

## Make categorical variables factors
varsToFactor &lt;- c(&quot;status&quot;,&quot;trt&quot;,&quot;ascites&quot;,&quot;hepato&quot;,&quot;spiders&quot;,&quot;edema&quot;,&quot;stage&quot;)
pbc[varsToFactor] &lt;- lapply(pbc[varsToFactor], factor)

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

## Create Table 1 stratified by trt
tableOne &lt;- CreateTableOne(vars = vars, strata = c(&quot;trt&quot;), data = pbc)

tableOne

Before

                      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 
...

You should try to adapt the following code for your own data format:

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

After

                      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   

huangapple
  • 本文由 发表于 2023年6月1日 10:33:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76378332.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定