首页 > 解决方案 > 在 R 中加入两个 ID 列缺失数据的数据集

问题描述

我需要加入两个data.frames 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 提供的评论是实际的解决方案。

已删除过时的问题块,以避免将来遇到此问题的任何人进一步混淆。

标签: rmissing-datarecord-linkage

解决方案


正如我在对问题的最新编辑中指出的那样,解决方案是执行fill(). 我发现,在实际的 RWD 中,最好在加入之前在每个数据集中使用id1and进行双重填充。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 行。


推荐阅读