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

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

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

问题

这是一个 tibble 的简要概述:

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

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

两个简单的 gt_summaries 输出如下:

test %>%
  select(
    c(
      Year,
      `Theatre time (mins)`
      )) %>%
  tbl_summary(
    by = Year
    )

test %>%
  select(
    c(
      Anaesthesia,
      `Theatre time (mins)`
      )) %>%
  tbl_summary(
    by = Anaesthesia
    )

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

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

和:

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

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

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

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

谢谢

英文:

Here is a brief overview of a tibble:

> test
# A tibble: 78 × 3
   Year  Anaesthesia         `Theatre time (mins)`
   <fct> <chr>                               <dbl>
 1 2015  General anaesthetic                   240
 2 2016  General anaesthetic                   270
 3 2016  General anaesthetic                   260
 4 2016  General anaesthetic                   320
 5 2017  General anaesthetic                   195
 6 2017  General anaesthetic                   210
 7 2017  General anaesthetic                   185
 8 2017  General anaesthetic                   185
 9 2017  General anaesthetic                   235
10 2017  General anaesthetic                   250
# ℹ 68 more rows
# ℹ 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:

test %>%
  select(
    c(
      Year,
      `Theatre time (mins)`
      )) %>%
  tbl_summary(
    by = Year
    )

test %>%
  select(
    c(
      Anaesthesia,
      `Theatre time (mins)`
      )) %>%
  tbl_summary(
    by = Anaesthesia
    )

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

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

theatreTimeData %>%
  select(
    c(
      年份,
      麻醉,
      手术时间
      )) %>%
  tbl_continuous(
    变量 = 手术时间
    )

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

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

谢谢,Daniel!

英文:

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

theatreTimeData %>%
  select(
    c(
      Year,
      Anaesthesia,
      theatreTime
      )) %>%
  tbl_continuous(
    variable = theatreTime
    )

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:

确定