0

I have two data frames which share same keys, and I can join them using left_join or other join functions to merge data frames. But I have same variable in both data frames and they are missing differently. Here is my example:

df1 <- data.frame(key1= c("A", "B" , "C", "D", "E","F", "G" , "H", "I", "J"), 
                  key2 = c("a", "b", "c", "d", "e","f", "g", "h", "i", "j"), 
                  foo = c(NA, NA, 21 , 25,21,22, NA, 23 , 25, NA))
df2 <- data.frame(key1= c("F", "G" , "A", "B", "J"), 
                      key2 = c("f", "g", "a", "b", "j"), 
                      foo = c(NA, NA, 21 , 25,21))

When I call left_join(df1, df2) it give me following data frame

   key1 key2 foo
1     A    a  NA
2     B    b  NA
3     C    c  21
4     D    d  25
5     E    e  21
6     F    f  22
7     G    g  NA
8     H    h  23
9     I    i  25
10    J    j  NA

As you can see, I have "A a" key pair that is missing, but df2 has observation with "A a" key pair and it is not missing. How can I join this in a right way?

Henrik
  • 65,555
  • 14
  • 143
  • 159
  • 1
    try `left_join(df1, df2, by = c('key1', 'key2')) %>% mutate(foo = coalesce(foo.x, foo.y)) %>% select(-c(foo.x, foo.y))` – Sotos Jun 18 '17 at 14:39
  • it should be joining them on what they share and nothing else, right? I might be misunderstanding the application in the question – sconfluentus Jun 18 '17 at 14:51
  • Sorry...thought missed the entirety of the question. I it will let me I will erase that comment. There, now it is just confusing to have a conversation about missing comments :) – sconfluentus Jun 18 '17 at 15:01
  • Thanks @Sotos that what I was searching for. I am sorry for making duplicate question, but still, it was very helpful. – Rafik Margaryan Jun 18 '17 at 17:18

0 Answers0