Table1() 在 R 中的描述性统计学

3

有没有table1用户知道如何按频率降序排序表格?例如在附加的表格中,您知道如何排序为14、11、7吗?

library(table1)
mtcars$cyl <- as.factor(mtcars$cyl)
table1(~mtcars$cyl, data = mtcars)

enter image description here

1个回答

3

一种方法是根据的频率排列因子水平。

mtcars$cyl <- factor(mtcars$cyl, levels = as.integer(names(sort(table(mtcars$cyl),
                                  decreasing = TRUE))))
table1::table1(~cyl, data = mtcars)

enter image description here


需要注意的是,如果您正在按字符串而不是数字排序因子变量,则应将 as.integer 更改为 as.factor - arielhasidim

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接