r - 在 R 中加入两个 ID 列缺失数据的数据集
问题描述
我需要加入两个data.frame
s in R
,并且他们都有两个 id 候选者,但是丢失了很多数据(所以我不能只选择一个并过滤掉其余的)。
编辑两个数据集的可重现示例和期望的结果:
messy1 <- data.frame(id1 = c(11, NA, 13, 11, 12, NA), id2 = c(NA, 22, 23, 21, NA, 23), time = rep(1:2, each = 3))
messy2 <- data.frame(id1 = c(12, NA, 14, 14), id2 = c(22, 23, 24, NA), time = c(1, 1, 1, 2))
desired1 <- data.frame(id1 = rep(11:13, 2), id2 = rep(21:23, 2), time = rep(1:2, each = 3)) # 12/22 is only available in messy2
desired2 <- data.frame(id1 = c(12:13, 14, 14), id2 = c(22:23, 24, 24), time = c(rep(1, 3), 2)) # 13/23 is only available in messy1
desired_joined <- full_join(desired1, desired2)
> messy1
id1 id2 time
1 11 NA 1
2 NA 22 1
3 13 23 1
4 11 21 2
5 12 NA 2
6 NA 23 2
> messy2
id1 id2 time
1 12 22 1
2 NA 23 1
3 14 24 1
4 14 NA 2
> desired_joined
id1 id2 time
1 11 21 1
2 12 22 1
3 13 23 1
4 11 21 2
5 12 22 2
6 13 23 2
7 14 24 1
8 14 24 2
最终编辑
上面的例子现在准确地反映了我试图解决的问题。在弄清楚这一点之后,@Ben 提供的评论是实际的解决方案。
已删除过时的问题块,以避免将来遇到此问题的任何人进一步混淆。
解决方案
正如我在对问题的最新编辑中指出的那样,解决方案是执行fill()
. 我发现,在实际的 RWD 中,最好在加入之前在每个数据集中使用id1
and进行双重填充。id2
任何需要做类似事情的人都可以使用以下步骤:
fill1 <- messy1 %>%
group_by(id1) %>% fill(id2, .direction = "downup") %>%
group_by(id2) %>% fill(id1, .direction = "downup") %>%
ungroup()
fill2 <- messy2 %>%
group_by(id1) %>% fill(id2, .direction = "downup") %>%
group_by(id2) %>% fill(id1, .direction = "downup") %>%
ungroup()
full_join(messy1, messy2) %>%
group_by(id1) %>% fill(id2, .direction = "downup") %>%
group_by(id2) %>% fill(id1, .direction = "downup") %>%
ungroup() %>%
distinct()
Joining, by = c("id1", "id2", "time")
# A tibble: 8 x 3
# Groups: id2 [4]
id1 id2 time
<dbl> <dbl> <dbl>
1 11 21 1
2 12 22 1
3 13 23 1
4 11 21 2
5 12 22 2
6 13 23 2
7 14 24 1
8 14 24 2
注意:我的实际数据集每个都有约 65k 行,但如果我join
在此之前fill
我最终有 250 万行(原因我不知道)。通过执行所有这些fill
操作,我最终得到了更合理的 73k 行。
推荐阅读
- azure - Azure 订阅迁移到新租户:保存 RBAC
- openvpn - 是否可以限制 azure vpn 用户在使用 AD auth 时仅连接公司设备
- html - 表格单元格内的自定义文本样式
- java - 我将如何去创建一个高级计算器?
- java - 当我遇到此问题时我该怎么办 JSP 问题:处理操作时出现 HTTP 500 内部服务器错误状态?
- python - 帖子详细信息视图上的 django 评论表单
- c - RPi 4 GPIO在上升和下降触发器上的中断在C中不起作用
- refactoring - 函数调用作为另一个函数参数(重构)
- c - 为什么长期运行的程序总是要收割他们的僵尸孩子?
- postgresql - 在 PostgreSQL 中创建日期后每 3 个月重复一次未来日期