创建三向表:tabyl 输出排序
Creating 3-way tables: tabyl output sorting
我正在尝试创建一个三向 tabyl,但我正在努力根据需要对列和行进行排序。这是示例:
library(janitor)
df <- data.frame(gender = c("f", "m", "m", "f", "m"),
age_group = factor(
c("35-45", "18-35", ">55", "45-55", ">55"),
levels = c("18-35", "35-45", "45-55", ">55")),
bmi_group = factor(
c("18.5 - 25", "<18.5", "18.5 - 25", ">30", "25 - 30"),
levels = c("<18.5", "18.5 - 25", "25 - 30", ">30")),
stringsAsFactors = TRUE)
df %>%
tabyl(bmi_group, age_group, gender, show_missing_levels = FALSE) %>%
adorn_totals("row") %>%
adorn_percentages("all") %>%
adorn_pct_formatting(digits = 1) %>%
adorn_ns %>%
adorn_title
它给了我这个:
我不知道如何按因素级别对特定性别表格中的条目进行排序。现在按字母顺序排序。
抓得好!这是一个 bug in the janitor package. It was fixed in version 2.1.0,于 2021 年 1 月发布。上面的相同代码现在生成列和行按因子级别排序的输出:
$f
age_group
bmi_group 35-45 45-55
18.5 - 25 50.0% (1) 0.0% (0)
>30 0.0% (0) 50.0% (1)
Total 50.0% (1) 50.0% (1)
$m
age_group
bmi_group 18-35 >55
<18.5 33.3% (1) 0.0% (0)
18.5 - 25 0.0% (0) 33.3% (1)
25 - 30 0.0% (0) 33.3% (1)
Total 33.3% (1) 66.7% (2)
我正在尝试创建一个三向 tabyl,但我正在努力根据需要对列和行进行排序。这是示例:
library(janitor)
df <- data.frame(gender = c("f", "m", "m", "f", "m"),
age_group = factor(
c("35-45", "18-35", ">55", "45-55", ">55"),
levels = c("18-35", "35-45", "45-55", ">55")),
bmi_group = factor(
c("18.5 - 25", "<18.5", "18.5 - 25", ">30", "25 - 30"),
levels = c("<18.5", "18.5 - 25", "25 - 30", ">30")),
stringsAsFactors = TRUE)
df %>%
tabyl(bmi_group, age_group, gender, show_missing_levels = FALSE) %>%
adorn_totals("row") %>%
adorn_percentages("all") %>%
adorn_pct_formatting(digits = 1) %>%
adorn_ns %>%
adorn_title
它给了我这个:
我不知道如何按因素级别对特定性别表格中的条目进行排序。现在按字母顺序排序。
抓得好!这是一个 bug in the janitor package. It was fixed in version 2.1.0,于 2021 年 1 月发布。上面的相同代码现在生成列和行按因子级别排序的输出:
$f
age_group
bmi_group 35-45 45-55
18.5 - 25 50.0% (1) 0.0% (0)
>30 0.0% (0) 50.0% (1)
Total 50.0% (1) 50.0% (1)
$m
age_group
bmi_group 18-35 >55
<18.5 33.3% (1) 0.0% (0)
18.5 - 25 0.0% (0) 33.3% (1)
25 - 30 0.0% (0) 33.3% (1)
Total 33.3% (1) 66.7% (2)