如何从gtsummary表中隐藏特定单元格的信息

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

How to mask certain cell's info from a gtsummary table

问题

我有一些敏感信息在我的数据中,我需要将其隐藏在某个特定的阈值以下(以符合数据使用协议并防止重新识别数据)。我正在使用gtsummary中的tbl_svysmmary()。在我的示例中,我想要过滤"cell size ≤ 100":

  1. library(gtsummary)
  2. library(survey)
  3. tbl_svysummary <-
  4. svydesign(~1, data = as.data.frame(Titanic), weights = ~Freq) %>%
  5. tbl_svysummary(by = Survived, percent = "row", include = c(Class, Age))
  6. tbl_svysummary

如何从gtsummary表中隐藏特定单元格的信息


我想显示儿童信息如下:

如何从gtsummary表中隐藏特定单元格的信息


编辑:用于多个stat_列和0/1变量的可重现示例:

  1. library(gtsummary)
  2. library(survey)
  3. supp_outcomes <-
  4. as.data.frame(Titanic) %>%
  5. mutate(Female = ifelse(Sex == "Female", 1, 0)) %>%
  6. svydesign(~1, data = ., weights = ~Freq) %>%
  7. tbl_svysummary(by = Survived, percent = "row",
  8. include = c(Age, Female, Class)) %>%
  9. add_overall() %>%
  10. add_p()
  11. supp_outcomes

从@Marco建议的解决方案中编辑的代码:

  1. supp_outcomes$table_body <- supp_outcomes$table_body %>%
  2. mutate(extra1 = stat_0,
  3. extra2 = stat_1,
  4. extra3 = stat_2) %>%
  5. separate(extra1, c("number"), sep = ' \\(') %>%
  6. separate(extra2, c("number"), sep = ' \\(') %>%
  7. separate(extra3, c("number"), sep = ' \\(') %>%
  8. mutate(number = as.numeric(number)) %>%
  9. mutate(stat_0 = case_when(
  10. number < 200 & number > 0 & var_type %in% c("dichotomous", "categorical") ~ "TOO FEW",
  11. TRUE ~ stat_0),
  12. stat_1 = case_when(
  13. number < 200 & number > 0 & var_type %in% c("dichotomous", "categorical") ~ "TOO FEW",
  14. TRUE ~ stat_1),
  15. stat_2 = case_when(
  16. number < 200 & number > 0 & var_type %in% c("dichotomous", "categorical") ~ "TOO FEW",
  17. TRUE ~ stat_2)) %>%
  18. select(!number)
  19. supp_outcomes
英文:

I have some sensitive info in my data that I need to hide below a certain threshold (to comply with DUA and prevent reidentifying data). I'm using tbl_svysmmary() from gtsummary. In my example, I like to filter "cell size ≤ 100":

  1. library(gtsummary)
  2. library(survey)
  3. tbl_svysummary &lt;-
  4. svydesign(~1, data = as.data.frame(Titanic), weights = ~Freq) %&gt;%
  5. tbl_svysummary(by = Survived, percent = &quot;row&quot;, include = c(Class, Age))
  6. tbl_svysummary

如何从gtsummary表中隐藏特定单元格的信息


I want to show child info as:

如何从gtsummary表中隐藏特定单元格的信息


EDIT: Reproducible example for multiple stat_ columns and 0/1 variables:

  1. library(gtsummary)
  2. library(survey)
  3. supp_outcomes &lt;-
  4. as.data.frame(Titanic) %&gt;%
  5. mutate (Female=ifelse(Sex==&quot;Female&quot;,1,0)) %&gt;%
  6. svydesign(~1, data = ., weights = ~Freq) %&gt;%
  7. tbl_svysummary(by = Survived, percent = &quot;row&quot;,
  8. include = c(Age, Female, Class)) %&gt;%
  9. add_overall() %&gt;% add_p()
  10. supp_outcomes

The edited code from @Marco's suggested solution:

  1. supp_outcomes$table_body &lt;- supp_outcomes$table_body %&gt;%
  2. mutate(extra1 = stat_0,
  3. extra2 = stat_1,
  4. extra3 = stat_2) %&gt;%
  5. separate(extra1, c(&quot;number&quot;), sep = &#39; \\(&#39;) %&gt;%
  6. separate(extra2, c(&quot;number&quot;), sep = &#39; \\(&#39;) %&gt;%
  7. separate( extra3, c(&quot;number&quot;), sep = &#39; \\(&#39;) %&gt;%
  8. mutate(number = as.numeric(number)) %&gt;%
  9. mutate(stat_0 = case_when(
  10. number &lt; 200 &amp; number &gt; 0 &amp; var_type %in%c(&quot;dichotomous&quot;, &quot;categorical&quot;)~ &quot;TOO FEW&quot;,
  11. TRUE ~ stat_0),
  12. stat_1 = case_when(
  13. number &lt; 200 &amp; number &gt; 0 &amp; var_type %in%c(&quot;dichotomous&quot;, &quot;categorical&quot;)~ &quot;TOO FEW&quot;,
  14. TRUE ~ stat_1),
  15. stat_2 = case_when(
  16. number &lt; 200 &amp; number &gt; 0 &amp; var_type %in%c(&quot;dichotomous&quot;, &quot;categorical&quot;)~ &quot;TOO FEW&quot;,
  17. TRUE ~ stat_2)) %&gt;%
  18. select(!number)
  19. supp_outcomes

答案1

得分: 1

你可以对输出对象进行更多的 tidyverse 操作,就像这样(使用 table_body):

  1. library(tidyverse)
  2. data(mtcars)
  3. library(gtsummary)
  4. output <- mtcars[,1:2] %>% tbl_summary()
  5. output$table_body
  6. # 一个 tibble: 5 × 6
  7. variable var_type var_label row_type label stat_0
  8. <chr> <chr> <chr> <chr> <chr> <chr>
  9. 1 mpg continuous mpg label mpg 19.2 (15.4, 22.8)
  10. 2 cyl categorical cyl label cyl NA
  11. 3 cyl categorical cyl level 4 11 (34%)
  12. 4 cyl categorical cyl level 6 7 (22%)
  13. 5 cyl categorical cyl level 8 14 (44%)
  14. # 当 cyl 的 N 少于 10 时,不显示单元格信息
  15. output$table_body <- output$table_body %>%
  16. mutate(extra = stat_0) %>%
  17. separate(extra, c("number"), sep = ' \\(') %>%
  18. mutate(number = as.numeric(number)) %>%
  19. mutate(stat_0 = case_when(number < 10 & var_type == "categorical" ~ "TOO FEW",
  20. TRUE ~ stat_0)) %>%
  21. select(!number)
  22. output

case_when 中的筛选决策基于分类变量,当单元格信息少于阈值时,你可以控制它。你可以根据其他变量或条件进行调整。

英文:

You can do more tidyverse manipulation on the output object like this (using the table_body):

  1. library(tidyverse)
  2. data(mtcars)
  3. library(gtsummary)
  4. output &lt;- mtcars[,1:2] %&gt;% tbl_summary()
  5. output$table_body
  6. # A tibble: 5 &#215; 6
  7. variable var_type var_label row_type label stat_0
  8. &lt;chr&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt;
  9. 1 mpg continuous mpg label mpg 19.2 (15.4, 22.8)
  10. 2 cyl categorical cyl label cyl NA
  11. 3 cyl categorical cyl level 4 11 (34%)
  12. 4 cyl categorical cyl level 6 7 (22%)
  13. 5 cyl categorical cyl level 8 14 (44%)
  14. # Don&#39;t show cell information when N of cyl is less than 10
  15. output$table_body &lt;- output$table_body %&gt;%
  16. mutate(extra = stat_0) %&gt;%
  17. separate(extra, c(&quot;number&quot;), sep = &#39; \\(&#39;) %&gt;%
  18. mutate(number = as.numeric(number)) %&gt;%
  19. mutate(stat_0 = case_when(number &lt; 10 &amp; var_type == &quot;categorical&quot; ~ &quot;TOO FEW&quot;,
  20. TRUE ~ stat_0)) %&gt;%
  21. select(!number)
  22. output

The filter decision in case_when is based on categorical variables, where you like to control the cell information, when it is less than a threshold value. You can adjust this for any other variable or condition.

如何从gtsummary表中隐藏特定单元格的信息

huangapple
  • 本文由 发表于 2023年2月14日 08:16:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75442357.html
匿名

发表评论

匿名网友

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

确定