使用下面的示例数据框 df
:
library(dplyr)
df <- data.frame(ID=c("A", "A", "B", "C", "C", "C"),
Color=c("red", "green", "red", "red", "blue", "red"),
Size=c("s", "s", "m", "m", "s", "m"),
Country=c("UK", "US", "US", "US", "AUS", "CA")) %>%
dplyr::arrange(across(everything()))
df
ID Color Size Country
1 A green s US
2 A red s UK
3 B red m US
4 C blue s AUS
5 C red m CA
6 C red m US
我使用 ID
中的 Color
函数按以下格式总结了每个 Size
以及每个 Country
和 tableby
的 arsenal
元素数量{1}} 包:
library(arsenal)
tableby(Color ~ Size + Country, df, test=F, total=F) %>%
summary()
| | blue (N=1) | green (N=1) | red (N=4) |
|:---------------------|:----------:|:-----------:|:---------:|
|**Size** | | | |
| m | 0 (0.0%) | 0 (0.0%) | 3 (75.0%) |
| s | 1 (100.0%) | 1 (100.0%) | 1 (25.0%) |
|**Country** | | | |
| AUS | 1 (100.0%) | 0 (0.0%) | 0 (0.0%) |
| CA | 0 (0.0%) | 0 (0.0%) | 1 (25.0%) |
| UK | 0 (0.0%) | 0 (0.0%) | 1 (25.0%) |
| US | 0 (0.0%) | 1 (100.0%) | 2 (50.0%) |
但是,我没有独立于其他因素获得每个辅因子的唯一 ID
值的计数(例如,只有 2 个唯一的 ID
- B
和 {{1} } - 颜色为 C
,大小为 red
,尽管确实有 3 行颜色为 m
,大小为 red
)。
有没有办法完成这个任务?