r - 总结两个数据框之间的差异
问题描述
我有两个不同的数据集,分别称为 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 包
谢谢
解决方案
# 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))
推荐阅读
- javascript - 无法同步主干 js 集合获取数据
- git - Git 是否对 core.autocrlf 和 .gitattributes text=auto 使用相同的启发式?
- crystal-reports - 如果一个数字在数组中每组出现一次,我如何计算
- c# - 在网格视图中启用特定行
- razor - 在 .Net Core 中导航到另一个文件夹中的页面
- javascript - 确定函数是否用“异步”定义
- visual-studio-code - 编程 Dart 时如何在 VSCode 中为我的类型着色和添加样式
- raspberry-pi3 - 使用我的树莓派连接到谷歌助手时出错
- google-cloud-dataflow - 如何在 Dataflow 中创建引用服务帐户 json 文件的 GoogleCredential 对象?
- c# - EF:空间/全文/哈希索引和显式索引顺序的错误使用