Home > database >  Merging two dataframes by keeping certain column values in r
Merging two dataframes by keeping certain column values in r

Time:12-17

I have two dataframes I need to merge with. The second one has certain columns missing and it also has some more ids. Here is how the sample datasets look like.

df1 <- data.frame(id = c(1,2,3,4,5,6),
                   item = c(11,22,33,44,55,66),
                   score = c(1,0,1,1,1,0),
                   cat.a = c("A","B","C","D","E","F"),
                   cat.b = c("a","a","b","b","c","f"))

> df1
  id item score cat.a cat.b
1  1   11     1     A     a
2  2   22     0     B     a
3  3   33     1     C     b
4  4   44     1     D     b
5  5   55     1     E     c
6  6   66     0     F     f

df2 <- data.frame(id = c(1,2,3,4,5,6,7,8),
                  item = c(11,22,33,44,55,66,77,88),
                  score = c(1,0,1,1,1,0,1,1),
                  cat.a = c(NA,NA,NA,NA,NA,NA,NA,NA),
                  cat.b = c(NA,NA,NA,NA,NA,NA,NA,NA))

> df2
  id item score cat.a cat.b
1  1   11     1    NA    NA
2  2   22     0    NA    NA
3  3   33     1    NA    NA
4  4   44     1    NA    NA
5  5   55     1    NA    NA
6  6   66     0    NA    NA
7  7   77     1    NA    NA
8  8   88     1    NA    NA

The two datasets share first 6 rows and dataset 2 has two more rows. When I merge I need to keep cat.a and cat.b information from the first dataframe. Then I also want to keep id=7 and id=8 with cat.a and cat.b columns missing.

Here is my desired output.

> df3
  id item score cat.a cat.b
1  1   11     1     A     a
2  2   22     0     B     a
3  3   33     1     C     b
4  4   44     1     D     b
5  5   55     1     E     c
6  6   66     0     F     f
7  7   77     1  <NA>  <NA>
8  8   88     1  <NA>  <NA>

Any ideas?

Thanks!

CodePudding user response:

We may use rows_update

library(dplyr)
rows_update(df2, df1, by = c("id", "item", "score"))

-output

  id item score cat.a cat.b
1  1   11     1     A     a
2  2   22     0     B     a
3  3   33     1     C     b
4  4   44     1     D     b
5  5   55     1     E     c
6  6   66     0     F     f
7  7   77     1  <NA>  <NA>
8  8   88     1  <NA>  <NA>
  • Related