首页 > 解决方案 > 总结两个数据框之间的差异

问题描述

我有两个不同的数据集,分别称为 Aug 和 Sept

请参阅下面的数据集示例。九月

九月
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900

和八月

奥古
9887
9888
9889
9890
9891
3223
3223
3223
3223
3223
3223
6563
6563
6563
6563
6563

我想要的是计算不在 9 月 2 日的 8 月数字的计数和百分比。计算 9 月不在 8 月的新数字以及 8 月和 9 月的数字和百分比

请记住,这是两个不同的数据帧。欢迎任何 R 包,但我更喜欢 tidyverse 或 dplyr 包

谢谢

标签: rdplyr

解决方案


# Count of numbers in August but not in September
nrow(anti_join(df1, df2, c('Augu' = 'Sept')))
[1] 11

# Count of numbers in September not in August
nrow(anti_join(df2, df1, c('Sept' = 'Augu')))
[1] 9

# Count of numbers in both August and September
nrow(inner_join(df2, df1, c('Sept' = 'Augu')))
[1] 5

数据

df1 <- structure(list(Augu = c(9887L, 9888L, 9889L, 9890L, 9891L, 3223L, 
3223L, 3223L, 3223L, 3223L, 3223L, 6563L, 6563L, 6563L, 6563L, 
6563L)), class = "data.frame", row.names = c(NA, -16L))

df2 <- structure(list(Sept = 9887:9900), class = "data.frame", row.names = c(NA, 
-14L))

推荐阅读