英文:
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))) %>%
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)
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 ~ "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)
<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 ~ "continuous2"),
statistic = list(all_continuous2() ~ c("{median} ({p25} - {p75})",
"{min} - {max}",
"{mean} ({sd})")),
include = c(Species, Sepal.Length, Sepal.Width),
digits = list(all_continuous2() ~ c(1, 2, 3, 4, 5, 6, 7))
) %>%
as.data.frame()
#> **Characteristic** **setosa**, N = 50 **versicolor**, N = 50
#> 1 Sepal.Length <NA> <NA>
#> 2 Median (IQR) 5.0 (4.80 - 5.200) 5.9 (5.60 - 6.300)
#> 3 Range 4.3000 - 5.80000 4.9000 - 7.00000
#> 4 Mean (SD) 5.006000 (0.3524897) 5.936000 (0.5161711)
#> 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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论