r - 在双向频率表中同时显示 Ns 和比例
问题描述
我正在尝试创建一个不符合“整洁”输出的发布表:
dummy <- data.frame(categorical_1 = c("a", "b", "a", "a", "b", "b", "a", "b", "b", "a"),
categorical_2 = c(rep("one", 5), rep("two", 5)),
numeric = sample(1:10, 10))
dummy %>%
count(categorical_1, categorical_2) %>%
group_by(categorical_1) %>%
mutate(prop = prop.table(n))
Tidyverse 输出
categorical_1 categorical_2 n prop
<fct> <fct> <int> <dbl>
1 a one 3 0.6
2 a two 2 0.4
3 b one 2 0.4
4 b two 3 0.6
期望的输出:
Category One Two
a 3 (0.6) 2 (0.4)
b 2 (0.4) 3 (0.6)
也许我可以应用其他mutate
步骤来使表格符合我想要的输出?
解决方案
library(janitor)
dummy %>%
tabyl(categorical_1, categorical_2) %>%
adorn_percentages("row") %>%
adorn_ns(position = "front")
#> categorical_1 one two
#> a 3 (0.6) 2 (0.4)
#> b 2 (0.4) 3 (0.6)
推荐阅读
- c# - C# - 使用 CSVContext Read() 和 Count() 从 CSV 读取已删除行的问题
- python - setup.py 如何安装包
- angular - Ionic4:将图像大小作为背景图像 url 的封面
- javascript - 在传递字符串 trim() 的 Vue.JS 方法中不起作用
- java - 列出 HADOOP 2.x 中映射器和缩减器数量的命令
- node.js - 将 mongodb 对象 ID 设置为“String”而不是“Schema.Types.ObjectId”是不好的做法吗?
- php - 为什么 $_GET 没有从不同的页面获取数据?
- java - 如何提取包含已字符串化为 Java 类的 ISO8385 字段以及匹配字段的 JSON 内容?
- python - 安装 2.7.5 时的段错误
- c++ - 从方法返回 JsonDoc 作为引用