[英]How to sort or change rows order in a сharacteristic table column in the gtsummary package?
I'm trying to change rows order in a сharacteristic table column using function sort = list (stage ~ "alphanumeric ") in the tbl_summary () trial[c("trt", "age", "stage", "grade")] %>%
tbl_summary(by = trt, sort = list (grade ~ "alphanumeric"))
.我正在尝试使用 tbl_summary ()
trial[c("trt", "age", "stage", "grade")] %>%
tbl_summary(by = trt, sort = list (grade ~ "alphanumeric"))
。 This does not work.这不起作用。 I would like to see (for example: stage T3, T 4, T1, T2 and grade III -> I )
我想看(例如:T3、T 4、T1、T2 和 III 级 -> I 阶段)
There are 3 ways to control the order levels of categorical variables appear in the tbl_summary()
output. tbl_summary()
output 中有 3 种方法可以控制分类变量的阶数。
Use the default alphanumeric sorting (factors are sorted by their factor level)使用默认的字母数字排序(因子按其因子级别排序)
Sort the output by frequency using the tbl_summary(sort=)
argument.使用
tbl_summary(sort=)
参数按频率对 output 进行排序。
Change the order by defining a factor variable and specifying the order you'd like the output to appear.通过定义因子变量并指定您希望 output 出现的顺序来更改顺序。
The examples below are for each of these cases.以下示例适用于每种情况。 I hope this answers your question!
我希望这回答了你的问题! Happy Coding!
快乐编码!
library(tidyverse)
library(gtsummary)
# sorting by alphanumeric is the default
trial[c("trt", "stage")] %>%
tbl_summary(by = trt)
# sorting by frequency using the `sort=` argument
trial[c("trt", "stage")] %>%
tbl_summary(by = trt, sort = all_categorical() ~ "frequency")
# manually change the order in the dataset, before passing to `tbl_summary`
trial[c("trt", "stage")] %>%
mutate(stage = factor(stage, levels = c("T4", "T3", "T2", "T1"))) %>%
tbl_summary(by = trt)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.