2

I have two datasets which they have common column names between them, but the values in them are sometimes shared between the datasets. As an example:

df1 <- data.frame(Name = c("Angus", "Angus", "Jason"), 
              Height=c("1.67", "1.67", "1.89"))
df2 <- data.frame(Name = c("Jack", "Brad", "Jason"), 
                  Weight=c("70", "75", "80"))

And I want to join them into a new data frame so that when there isn't a common value between them such as Angus in the Name column, it would be filled with NAs. My desire example output:

df3 <- data.frame(Name = c("Angus","Angus","Jack", "Brad", "Jason"), 
                  Height=c("1.69", "1.73", "NA","NA","1.89"),
                  Weight=c("NA","NA","70", "75", "80"))

I am not posting my original dataset because is a big dataset but this simple example perfectly illustrate what I'm desiring.

I allready tried using the merge() function with fill = NA but it isn't what I was wanting.

shrindella
  • 81
  • 4
  • Possible duplicate https://stackoverflow.com/questions/1299871/how-to-join-merge-data-frames-inner-outer-left-right – Ronak Shah Nov 28 '19 at 01:05

2 Answers2

1

You may want to use:

merge(df1, df2, all = TRUE)

   Name Height Weight
1 Angus   1.67   <NA>
2 Angus   1.67   <NA>
3 Jason   1.89     80
4  Brad   <NA>     75
5  Jack   <NA>     70

From documentation:

In SQL database terminology, the default value of all = FALSE gives a natural join, a special case of an inner join. Specifying all.x = TRUE gives a left (outer) join, all.y = TRUE a right (outer) join, and both (all = TRUE) a (full) outer join. DBMSes do not match NULL records, equivalent to incomparables = NA in R.

tmfmnk
  • 38,881
  • 4
  • 47
  • 67
0

We can use full_join from dplyr

library(dplyr)
full_join(df1, df2)
#   Name Height Weight
#1 Angus   1.67   <NA>
#2 Angus   1.67   <NA>
#3 Jason   1.89     80
#4  Jack   <NA>     70
#5  Brad   <NA>     75
akrun
  • 874,273
  • 37
  • 540
  • 662