0

I'm just a bit new with R and I have to work with it. I have 2 data frames: df1 and df2

x |  y |  z   | q                 x | y  |  w
1 | 00 | 1.99 | 5                 1 | 00 | 1.34
1 | 10 | 2.05 | 11                1 | 12 | 1.69
1 | 12 | 1.89 | 9                 2 | 15 | 2.99
1 | 20 | 1.75 | 7
2 | 05 | 3.25 | 3
2 | 15 | 3.35 | 0
2 | 26 | 3.10 | 6

And I would like to create a new data frame (ndf) combining df1 and df2 like this:

 x |  y |  z   | q  |  w             
 1 | 00 | 1.99 | 5  | 1.34            
 1 | 10 | 2.05 | 11 |  NA              
 1 | 12 | 1.89 | 9  | 1.69        
 1 | 20 | 1.75 | 7  |  NA
 2 | 05 | 3.25 | 3  |  NA
 2 | 15 | 3.35 | 0  | 2.99
 2 | 26 | 3.10 | 6  |  NA

How can I obtain this dataframe in R? Someone can help me please?

Sven Hohenstein
  • 80,497
  • 17
  • 145
  • 168
Mig
  • 23
  • 1
  • 1
  • 5

1 Answers1

-1

You can use merge to combine two data frames.

merge(df1, df2, all = TRUE)

The result:

  x  y    z  q    w
1 1  0 1.99  5 1.34
2 1 10 2.05 11   NA
3 1 12 1.89  9 1.69
4 1 20 1.75  7   NA
5 2  5 3.25  3   NA
6 2 15 3.35  0 2.99
7 2 26 3.10  6   NA
Sven Hohenstein
  • 80,497
  • 17
  • 145
  • 168