合并 R 中缺少值的两个数据帧

Merging two dataframes in R with missing values

我有两个如下所示的数据框:

df_1 <- tibble(id = c(1,1,2,2,3,3), y = c("a", "b", "a", "b","a", "b"))
df_2 <- tibble(id = c(1,1,3,3), z = c(4,5,6,5))

我想合并两个 dfs,如下所示:

df_3 <- tibble(id = c(1,1,2,2,3,3), y = c("a", "b", "a", "b","a", "b"), z = c(4,5,NA,NA,6,5))

我如何在 R 中执行此操作?谢谢!

通过 'id' 创建一个序列列,然后加入(因为 'id' 在两个数据集中都有重复项)

library(dplyr)
library(data.table)
df_1 %>% 
   mutate(rn = rowid(id)) %>%
   left_join(df_2 %>% 
      mutate(rn = rowid(id))) %>% 
    select(-rn)

-输出

# A tibble: 6 × 3
     id y         z
  <dbl> <chr> <dbl>
1     1 a         4
2     1 b         5
3     2 a        NA
4     2 b        NA
5     3 a         6
6     3 b         5
left_join(df_1, cbind(df_1 %>% filter(id %in% df_2$id), df_2 %>% select(z)))

输出:

# A tibble: 6 x 3
     id y         z
  <dbl> <chr> <dbl>
1     1 a         4
2     1 b         5
3     2 a        NA
4     2 b        NA
5     3 a         6
6     3 b         5