gtsummary: 控制连续变量的小数位数2

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

gtsummary: Controlling digits for a continuous2 variable

问题

gtsummary 中,连续变量只有一行与其相关的统计数据,并且每个输出数字的小数位数由提供的数字向量控制,该向量的整数数量与要进行样式化的数字数量相同:

library(gtsummary)

tbl_summary(iris, 
            by = Species,
            digits = list(all_continuous() ~ c(3, 2, 1))) %>%
    as.data.frame()

#>   **Characteristic** **setosa**, N = 50 **versicolor**, N = 50
#> 1       Sepal.Length  5.000 (4.80, 5.2)      5.900 (5.60, 6.3)
#> 2        Sepal.Width  3.400 (3.20, 3.7)      2.800 (2.53, 3.0)
#> 3       Petal.Length  1.500 (1.40, 1.6)      4.350 (4.00, 4.6)
#> 4        Petal.Width  0.200 (0.20, 0.3)      1.300 (1.20, 1.5)

但对于 type = continuous2 变量,gtsummary 为单个变量生成多行统计数据。如何控制这些统计数据的小数位数呢?

library(gtsummary)

tbl_summary(iris, 
            by = Species,
            type = list(Sepal.Length ~ "continuous2"),
            statistic = list(all_continuous2() ~ c("{median} ({p25} - {p75})",
                                                   "{min} - {max}",
                                                   "{mean} ({sd})")),
            include = c(Species, Sepal.Length, Sepal.Width)
            ) %>%
    as.data.frame()

#>   **Characteristic** **setosa**, N = 50 **versicolor**, N = 50
#> 1       Sepal.Length               <NA>                   <NA>
#> 2       Median (IQR) 5.00 (4.80 - 5.20)     5.90 (5.60 - 6.30)
#> 3              Range        4.30 - 5.80            4.90 - 7.00
#> 4          Mean (SD)        5.01 (0.35)            5.94 (0.52)
#> 5        Sepal.Width  3.40 (3.20, 3.68)      2.80 (2.53, 3.00)

创建于2023年06月05日,使用 reprex v2.0.2

英文:

In gtsummary, continuous variables have only one line of statistics associated with them, and the number of decimal places for each output number is controlled by offering a numeric vector that has as many integers as there are numbers to style:

library(gtsummary)

tbl_summary(iris, 
            by = Species,
            digits = list(all_continuous() ~ c(3, 2, 1))) %&gt;% 
    as.data.frame()

#&gt;   **Characteristic** **setosa**, N = 50 **versicolor**, N = 50
#&gt; 1       Sepal.Length  5.000 (4.80, 5.2)      5.900 (5.60, 6.3)
#&gt; 2        Sepal.Width  3.400 (3.20, 3.7)      2.800 (2.53, 3.0)
#&gt; 3       Petal.Length  1.500 (1.40, 1.6)      4.350 (4.00, 4.6)
#&gt; 4        Petal.Width  0.200 (0.20, 0.3)      1.300 (1.20, 1.5)

But for type = continuous2 variables, gtsummary generates several lines of statistics for a single variable. How do you control decimal places for these?

library(gtsummary)

tbl_summary(iris, 
            by = Species,
            type = list(Sepal.Length ~ &quot;continuous2&quot;),
            statistic = list(all_continuous2() ~ c(&quot;{median} ({p25} - {p75})&quot;, 
                                                   &quot;{min} - {max}&quot;,
                                                   &quot;{mean} ({sd})&quot;)),
            include = c(Species, Sepal.Length, Sepal.Width)
            ) %&gt;% 
    as.data.frame()

#&gt;   **Characteristic** **setosa**, N = 50 **versicolor**, N = 50
#&gt; 1       Sepal.Length               &lt;NA&gt;                   &lt;NA&gt;
#&gt; 2       Median (IQR) 5.00 (4.80 - 5.20)     5.90 (5.60 - 6.30)
#&gt; 3              Range        4.30 - 5.80            4.90 - 7.00
#&gt; 4          Mean (SD)        5.01 (0.35)            5.94 (0.52)
#&gt; 5        Sepal.Width  3.40 (3.20, 3.68)      2.80 (2.53, 3.00)

<sup>Created on 2023-06-05 with reprex v2.0.2</sup>

答案1

得分: 1

以下是您要翻译的部分:

你只需将数字添加到向量中的数字以匹配您想要在所有“continuous2”行上进行样式化的数字:

library(gtsummary)

tbl_summary(iris, 
            by = Species,
            type = list(Sepal.Length ~ "continuous2"),
            statistic = list(all_continuous2() ~ c("中位数(IQR) {median} ({p25} - {p75})",
                                                   "范围 {min} - {max}",
                                                   "平均值(SD) {mean} ({sd})")),
            include = c(Species, Sepal.Length, Sepal.Width),
            digits = list(all_continuous2() ~ c(1, 2, 3, 4, 5, 6, 7))
            ) %>% 
    as.data.frame()

#>   **特征**   **setosa**,N = 50 **versicolor**,N = 50
#> 1       花萼长度                 <NA>                   <NA>
#> 2       中位数(IQR)   5.0 (4.80 - 5.200)     5.9 (5.60 - 6.300)
#> 3              范围     4.3000 - 5.80000       4.9000 - 7.00000
#> 4          平均值(SD) 5.006000 (0.3524897)   5.936000 (0.5161711)
#> 5        花萼宽度    3.40 (3.20, 3.68)      2.80 (2.53, 3.00)

创建于2023年06月05日,使用reprex v2.0.2


<details>
<summary>英文:</summary>

You just keep adding numbers to the vector in digits to match the numbers you want to style across all of the `continuous2` rows:

``` r
library(gtsummary)

tbl_summary(iris, 
            by = Species,
            type = list(Sepal.Length ~ &quot;continuous2&quot;),
            statistic = list(all_continuous2() ~ c(&quot;{median} ({p25} - {p75})&quot;, 
                                                   &quot;{min} - {max}&quot;,
                                                   &quot;{mean} ({sd})&quot;)),
            include = c(Species, Sepal.Length, Sepal.Width),
            digits = list(all_continuous2() ~ c(1, 2, 3, 4, 5, 6, 7))
            ) %&gt;% 
    as.data.frame()

#&gt;   **Characteristic**   **setosa**, N = 50 **versicolor**, N = 50
#&gt; 1       Sepal.Length                 &lt;NA&gt;                   &lt;NA&gt;
#&gt; 2       Median (IQR)   5.0 (4.80 - 5.200)     5.9 (5.60 - 6.300)
#&gt; 3              Range     4.3000 - 5.80000       4.9000 - 7.00000
#&gt; 4          Mean (SD) 5.006000 (0.3524897)   5.936000 (0.5161711)
#&gt; 5        Sepal.Width    3.40 (3.20, 3.68)      2.80 (2.53, 3.00)

<sup>Created on 2023-06-05 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年6月5日 06:57:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76402723.html
匿名

发表评论

匿名网友

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

确定