0

What I am trying to do is to merge two data frames with observations of different variables by data.

The first data frame contains observations for the period between January 2019 till January 2020 The second data frame contains observations for the period between January 2020 till December 2020. Namely, the observations for the month of January 2020 are present in both dataframes. In the example below two of the last observation in dataframe 1 are identical to the two first observations in dataframe 2.

      dataframe 1
Date         V1    V2
2019-01-01    x     y
2019-01-03    x     z
2020-01-01    x     y
2020-01-02    v     x


     dataframe 2 
 Date         V1    V2
2020-01-01    x     y
2020-01-02    v     x
2020-01-03    v     x
2020-01-04    n     j
2020-01-06    b     h

Out of these two data frames I want to create one dataframe that spans from January 2019 till December 2020. Namely, from the beginning of dataframe 1 till the end of dataframe 2.

But, the goal is to merge the two dataframes in a way that if there are any same observations then only one observation of the two will remain in the new dataframe. While if there is an observation that is present in only one of the original dataframes it will remain in the new one. In other words, I want to avoid duplicates but preserve unique observations. I think its called full join. The new one should like like this

     NewDataFRAME
2019-01-01    x     y
2019-01-03    x     z
2020-01-01    x     y
2020-01-02    v     x
2020-01-03    v     x
2020-01-04    n     j
2020-01-06    b     h

The observations presented in both of the original dataframes are present only once in the new one.

I am not familiar with the correct terminology if you wish to feel free to explain it.

Ron
  • 65
  • 1
  • 6
  • Done. Hope it clear. – Ron Jan 19 '20 at 22:18
  • I posted a solution below – akrun Jan 19 '20 at 22:20
  • Would `merge(df1, df2, all = T)` give you what you need? A post to see if you haven't already on joins is [here](https://stackoverflow.com/questions/1299871/how-to-join-merge-data-frames-inner-outer-left-right). – Ben Jan 19 '20 at 22:27

1 Answers1

0

We can use rbind to bind the datasets together and apply unique on it

unique(rbind(df1, df2))
#         Date V1 V2
#1 2019-01-01  x  y
#2 2019-01-03  x  z
#3 2020-01-01  x  y
#4 2020-01-02  v  x
#7 2020-01-03  v  x
#8 2020-01-04  n  j
#9 2020-01-06  b  h

data

df1 <- structure(list(Date = c("2019-01-01", "2019-01-03", "2020-01-01", 
"2020-01-02"), V1 = c("x", "x", "x", "v"), V2 = c("y", "z", "y", 
"x")), class = "data.frame", row.names = c(NA, -4L))

df2 <- structure(list(Date = c("2020-01-01", "2020-01-02", "2020-01-03", 
"2020-01-04", "2020-01-06"), V1 = c("x", "v", "v", "n", "b"), 
    V2 = c("y", "x", "x", "j", "h")), class = "data.frame", row.names = c(NA, 
-5L))
akrun
  • 874,273
  • 37
  • 540
  • 662