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

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

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:

  1. 我使用 `library(tableone)` 来生成多个变量的描述性统计数据。
  2. 这是我的代码:
  3. library(tableone)
  4. myVars <- c("class", "age", "Sex", "bmi", "bmi_category",
  5. "drink_freq", "smoke_yn", "edu_dummy")
  6. catVars <- c("class", "Sex", "bmi_category",
  7. "drink_freq", "smoke_yn", "edu_dummy")
  8. tab1_inf <- CreateTableOne(vars = myVars, strata = "NEWDI",
  9. data = TKA_table1, factorVars = catVars)
  10. 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:

  1. library(tableone)
  2. myVars <- c("class", "age", "Sex", "bmi", "bmi_category",
  3. "drink_freq", "smoke_yn", "edu_dummy")
  4. catVars <- c("class", "Sex", "bmi_category",
  5. "drink_freq", "smoke_yn", "edu_dummy")
  6. tab1_inf <- CreateTableOne(vars = myVars, strata = "NEWDI",
  7. data = TKA_table1, factorVars = catVars)
  8. 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包。

  1. library(tableone)
  2. library(survival)
  3. data(pbc)
  4. ## 将分类变量转换为因子
  5. varsToFactor <- c("status","trt","ascites","hepato","spiders","edema","stage")
  6. pbc[varsToFactor] <- lapply(pbc[varsToFactor], factor)
  7. ## 创建变量列表
  8. vars <- c("time","status","age","sex","ascites","hepato",
  9. "spiders","edema","bili","chol","albumin",
  10. "copper","alk.phos","ast","trig","platelet",
  11. "protime","stage")
  12. ## 创建按trt分层的Table 1
  13. tableOne <- CreateTableOne(vars = vars, strata = c("trt"), data = pbc)
  14. tableOne

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

  1. for (i in 1:length(table1)) {
  2. sum = tableOne$CatTable[[1]][[i]]$freq + tableOne$CatTable[[2]][[i]]$freq
  3. tableOne$CatTable[[1]][[i]]$percent = tableOne$CatTable[[1]][[i]]$freq / sum
  4. tableOne$CatTable[[2]][[i]]$percent = tableOne$CatTable[[2]][[i]]$freq / sum
  5. }
  6. }
  7. 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.

  1. library(tableone)
  2. library(survival)
  3. data(pbc)
  4. ## Make categorical variables factors
  5. varsToFactor &lt;- c(&quot;status&quot;,&quot;trt&quot;,&quot;ascites&quot;,&quot;hepato&quot;,&quot;spiders&quot;,&quot;edema&quot;,&quot;stage&quot;)
  6. pbc[varsToFactor] &lt;- lapply(pbc[varsToFactor], factor)
  7. ## Create a variable list
  8. vars &lt;- c(&quot;time&quot;,&quot;status&quot;,&quot;age&quot;,&quot;sex&quot;,&quot;ascites&quot;,&quot;hepato&quot;,
  9. &quot;spiders&quot;,&quot;edema&quot;,&quot;bili&quot;,&quot;chol&quot;,&quot;albumin&quot;,
  10. &quot;copper&quot;,&quot;alk.phos&quot;,&quot;ast&quot;,&quot;trig&quot;,&quot;platelet&quot;,
  11. &quot;protime&quot;,&quot;stage&quot;)
  12. ## Create Table 1 stratified by trt
  13. tableOne &lt;- CreateTableOne(vars = vars, strata = c(&quot;trt&quot;), data = pbc)
  14. tableOne

Before

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

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

  1. for (i in 1:length(table1)) {
  2. sum = tableOne$CatTable[[1]][[i]]$freq + tableOne$CatTable[[2]][[i]]$freq
  3. tableOne$CatTable[[1]][[i]]$percent = tableOne$CatTable[[1]][[i]]$freq / sum
  4. tableOne$CatTable[[2]][[i]]$percent = tableOne$CatTable[[2]][[i]]$freq / sum
  5. }
  6. }
  7. tableOne

After

  1. Stratified by trt
  2. 1 2 p test
  3. n 158 154
  4. time (mean (SD)) 2015.62 (1094.12) 1996.86 (1155.93) 0.883
  5. status (%) 0.894
  6. 0 83 (0.5) 85 (0.5)
  7. 1 10 (0.5) 9 (0.5)
  8. 2 65 (0.5) 60 (0.5)
  9. age (mean (SD)) 51.42 (11.01) 48.58 (9.96) 0.018
  10. sex = f (%) 137 (0.5) 139 (0.5) 0.421
  11. ascites = 1 (%) 14 (0.6) 10 (0.4) 0.567
  12. hepato = 1 (%) 73 (0.5) 87 (0.5) 0.088
  13. 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:

确定