<p>Assuming your column ID in both dataframes <code>a</code> and <code>b</code> shared same IDs, and as your dataframe are not particularly heavy, you can have the use of the function <code>left_join</code> from <code>dplyr</code> package as this:</p>

<pre class="lang-r prettyprint-override"><code>library(dplyr)
DF &lt;- left_join(a,b, by = "ID")
</code></pre>

CC BY-SA 4.0
2020-01-20T03:11:31.550Z
2020-01-20T03:11:31.550Z
dc37
<p>@dc37's dplyr solution is quite elegant, but you can also use base R's merge:</p>

<pre><code>merge(x = df_a, y = df_b, by = "ID", all.x = TRUE, all.y = TRUE)
</code></pre>

<p>You can tweak various parameters, such as use <code>by.x</code> and <code>by.y</code> instead of just <code>by</code>, change <code>all.x</code> and <code>all.y</code> based on requirements to get your results.</p>

<p>This is a pretty common R question by the way, and if you had googled "merging data frames", you'd see a lot of options.</p>

CC BY-SA 4.0
2020-01-20T04:18:49.043Z
2020-01-20T04:18:49.043Z
Ram RS
<p>I have a data frame like with more samples like</p>

<pre><code>&gt; head(a[1:2,1:4])
             ID               ids pre.or.post.treatment
1 OCCAMS/AH/120 LP6005409-DNA_F01                   pre
2 OCCAMS/AH/126 LP2000325-DNA_A01                   pre
            TRG
1 non-responder
2 non-responder
&gt; dim(a)
[1] 160   4
&gt; 
</code></pre>

<p>Another data frame with part of my samples but more characteristics for each sample like</p>

<pre><code>&gt; dim(b)
[1] 60 50

&gt; head(b[1:2,1:6])
             ID       TRG pre.or.post.treatment          ID.1
1 OCCAMS/AH/212 responder                   pre OCCAMS/AH/212
2 OCCAMS/AH/279 responder                   pre OCCAMS/AH/279
  DiagnosisDate FE.LastSeenDate
1      8/8/2013        6/6/2014
2     6/10/2014       11/8/2018
&gt;  
</code></pre>

<p>How I can  merge these data frame getting a new data frame with my all 160 samples columns of both a and b</p>

<r>
Merging two data frames
# Merging two data frames

(Asked by: username_0 on 2020-01-19)

I have a data frame like with more samples like
> head(a[1:2,1:4])
             ID               ids pre.or.post.treatment
1 OCCAMS/AH/120 LP6005409-DNA_F01                   pre
2 OCCAMS/AH/126 LP2000325-DNA_A01                   pre
            TRG
1 non-responder
2 non-responder
> dim(a)
[1] 160   4
> 

Another data frame with part of my samples but more characteristics for each sample like
> dim(b)
[1] 60 50

> head(b[1:2,1:6])
             ID       TRG pre.or.post.treatment          ID.1
1 OCCAMS/AH/212 responder                   pre OCCAMS/AH/212
2 OCCAMS/AH/279 responder                   pre OCCAMS/AH/279
  DiagnosisDate FE.LastSeenDate
1      8/8/2013        6/6/2014
2     6/10/2014       11/8/2018
>  

How I can  merge these data frame getting a new data frame with my all 160 samples columns of both a and b

**Tags:** r

---

## 2 answers

### Answer by username_1 on 2020-01-20. Score: 3 | Accepted answer

Assuming your column ID in both dataframes a and b shared same IDs, and as your dataframe are not particularly heavy, you can have the use of the function left_join from dplyr package as this:
library(dplyr)
DF <- left_join(a,b, by = "ID")


---

### Answer by username_2 on 2020-01-20. Score: 3

@dc37's dplyr solution is quite elegant, but you can also use base R's merge:
merge(x = df_a, y = df_b, by = "ID", all.x = TRUE, all.y = TRUE)

You can tweak various parameters, such as use by.x and by.y instead of just by, change all.x and all.y based on requirements to get your results.
This is a pretty common R question by the way, and if you had googled "merging data frames", you'd see a lot of options.


---

