汇总表独立于其他变量计算唯一条目

时间:2021-04-16 17:13:19

标签: r

使用下面的示例数据框 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 以及每个 Countrytablebyarsenal 元素数量{1}} 包:

library(arsenal)

tableby(Color ~ Size + Country, df, test=F, total=F) %>% 
summary()
|                      | blue (N=1) | green (N=1) | red (N=4) |
|:---------------------|:----------:|:-----------:|:---------:|
|**Size**              |            |             |           |
|&nbsp;&nbsp;&nbsp;m   |  0 (0.0%)  |  0 (0.0%)   | 3 (75.0%) |
|&nbsp;&nbsp;&nbsp;s   | 1 (100.0%) | 1 (100.0%)  | 1 (25.0%) |
|**Country**           |            |             |           |
|&nbsp;&nbsp;&nbsp;AUS | 1 (100.0%) |  0 (0.0%)   | 0 (0.0%)  |
|&nbsp;&nbsp;&nbsp;CA  |  0 (0.0%)  |  0 (0.0%)   | 1 (25.0%) |
|&nbsp;&nbsp;&nbsp;UK  |  0 (0.0%)  |  0 (0.0%)   | 1 (25.0%) |
|&nbsp;&nbsp;&nbsp;US  |  0 (0.0%)  | 1 (100.0%)  | 2 (50.0%) |

但是,我没有独立于其他因素获得每个辅因子的唯一 ID 值的计数(例如,只有 2 个唯一的 ID - B 和 {{1} } - 颜色为 C,大小为 red,尽管确实有 3 行颜色为 m,大小为 red)。 有没有办法完成这个任务?

0 个答案:

没有答案