R语言统计df2中各(Measures,Format)组合在df1中的缺失数量
实现代码
你可以通过先统计df1各组合的实际题量,再关联df2计算差值实现需求,完整代码如下:
library(tidyverse) # 1. 统计df1中每个(Measures, Format)组合的实际存在题量 df1_cnt <- df1 %>% count(Measures, Format, name = "actual_count") # 2. 关联df2计算缺失量 res <- df2 %>% left_join(df1_cnt, by = c("Measures", "Format")) %>% # df1中完全不存在的组合默认实际数量为0 mutate(actual_count = replace_na(actual_count, 0), # 缺失量 = 需求数 - 实际数,若实际数超过需求则缺失量为0 missing_count = pmax(Number - actual_count, 0))
输出示例
基于你提供的示例数据,运行后得到的res结果如下:
| Measures | Format | Number | actual_count | missing_count |
|---|---|---|---|---|
| space and shape | Constructed Response Expert | 40 | 9 | 31 |
| space and shape | Constructed Response Manual | 1 | 1 | 0 |
| space and shape | Simple Multiple Choice | 22 | 6 | 16 |
| space and shape | Constructed Response Auto-coded | 1 | 1 | 0 |
| asdaf | asfas | 0 | 0 | 0 |
完全符合你举例的需求:space and shape + Constructed Response Expert组合缺失31道题。
内容的提问来源于stack exchange,提问作者ujjwal tyagi
相关产品推荐
相关产品推荐

