如何在使用gt_summary()的输出中翻转列和行?

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

How can I flip the columns and rows of this output from gt_summary()?

问题

这是一个 tibble 的简要概述:

  1. > test
  2. # A tibble: 78 × 3
  3. Year Anaesthesia `Theatre time (mins)`
  4. <fct> <chr> <dbl>
  5. 1 2015 General anaesthetic 240
  6. 2 2016 General anaesthetic 270
  7. 3 2016 General anaesthetic 260
  8. 4 2016 General anaesthetic 320
  9. 5 2017 General anaesthetic 195
  10. 6 2017 General anaesthetic 210
  11. 7 2017 General anaesthetic 185
  12. 8 2017 General anaesthetic 185
  13. 9 2017 General anaesthetic 235
  14. 10 2017 General anaesthetic 250
  15. # ℹ 68 more rows
  16. # ℹ Use `print(n = ...)` to see more rows

我想要显示不同 'year' 和 'anaesthesia' 类别的中位数和 IQR 手术时间。

两个简单的 gt_summaries 输出如下:

  1. test %>%
  2. select(
  3. c(
  4. Year,
  5. `Theatre time (mins)`
  6. )) %>%
  7. tbl_summary(
  8. by = Year
  9. )
  10. test %>%
  11. select(
  12. c(
  13. Anaesthesia,
  14. `Theatre time (mins)`
  15. )) %>%
  16. tbl_summary(
  17. by = Anaesthesia
  18. )

给我一个类似我想要的输出:

如何在使用gt_summary()的输出中翻转列和行?

和:

如何在使用gt_summary()的输出中翻转列和行?

这些都很好,但我觉得行和列最好互换。特别是因为我可能想要有第二个基于时间的测量来生成另一个中位数(IQR)列。

但目前,我想要一个总结表格,其中 Year 和 Anaesthesia 作为 'Characteristics'(带有它们自己的 'levels')和 Theatre time 的中位数(IQR)作为统计数据。我确信这最终将很简单,我只是缺乏语法技能。

我已经尝试过多次,努力思考并多次搜索,但没有找到答案或足够相似的问题可以自己解决。

谢谢

英文:

Here is a brief overview of a tibble:

  1. > test
  2. # A tibble: 78 × 3
  3. Year Anaesthesia `Theatre time (mins)`
  4. <fct> <chr> <dbl>
  5. 1 2015 General anaesthetic 240
  6. 2 2016 General anaesthetic 270
  7. 3 2016 General anaesthetic 260
  8. 4 2016 General anaesthetic 320
  9. 5 2017 General anaesthetic 195
  10. 6 2017 General anaesthetic 210
  11. 7 2017 General anaesthetic 185
  12. 8 2017 General anaesthetic 185
  13. 9 2017 General anaesthetic 235
  14. 10 2017 General anaesthetic 250
  15. # ℹ 68 more rows
  16. # ℹ Use `print(n = ...)` to see more rows

I want to display median & IQR theatre times for the different categories of 'year' and 'anaesthesia'

Two simple gt_summaries for output:

  1. test %>%
  2. select(
  3. c(
  4. Year,
  5. `Theatre time (mins)`
  6. )) %>%
  7. tbl_summary(
  8. by = Year
  9. )
  10. test %>%
  11. select(
  12. c(
  13. Anaesthesia,
  14. `Theatre time (mins)`
  15. )) %>%
  16. tbl_summary(
  17. by = Anaesthesia
  18. )

Give me output quite like what I want:

如何在使用gt_summary()的输出中翻转列和行?

and:

如何在使用gt_summary()的输出中翻转列和行?

These are nice, but I feel that the rows and columns would be better flipped. Especially as I would potentially like to have a second time based measurement to make another median (IQR) column out of.

But for now, I would like ONE summary table with Year and Anaesthesia as the 'Characteristics' (with their own 'levels') and median (IQR) of Theatre time as the statistic. I am certain this will ultimately be straightforward and I just lack the syntax skills.

I have tried and wrangled my brain and searched repeatedly but haven't found the answer or a similar enough question to work this out on my own.

Thanks

答案1

得分: 1

已解决,感谢回复。以下是我的输入(略有不同的半字节和变量名称):

  1. theatreTimeData %>%
  2. select(
  3. c(
  4. 年份,
  5. 麻醉,
  6. 手术时间
  7. )) %>%
  8. tbl_continuous(
  9. 变量 = 手术时间
  10. )

和输出(只需要一些美化,我会自己处理):

如何在使用gt_summary()的输出中翻转列和行?

谢谢,Daniel!

英文:

Solved, thanks to the reply. Here is my input (slightly different nibble & variable names):

  1. theatreTimeData %>%
  2. select(
  3. c(
  4. Year,
  5. Anaesthesia,
  6. theatreTime
  7. )) %>%
  8. tbl_continuous(
  9. variable = theatreTime
  10. )

And output (just needing some prettification which I'll work on myself):

如何在使用gt_summary()的输出中翻转列和行?

Thanks, Daniel!

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

发表评论

匿名网友

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

确定