<p>Your approach was correct, you just have to transform your dataframe first to replace the character &quot;-&quot; by NA :</p>
<pre><code>library(stringr)
library(dplyr)
df=data.frame(CDR3.light.aa=c(&quot;-&quot;,&quot;-&quot;,&quot;CSSYAGSNTFGLF&quot;,'CSSYAGSNTGLF'),
              CDR3.heavy.aa=c(&quot;CAGDLSVGGVLAGGVPHLRHFDPW&quot;,&quot;-&quot;,&quot;CPETPRYCSGGFCYGEFDNW&quot;,&quot;-&quot;))

df=df %&gt;% 
  mutate_all(funs(str_replace(., &quot;-&quot;, &quot;NA&quot;)))      


df<span class="math-container">$a &lt;- paste(df$</span>CDR3.heavy.aa, df$CDR3.light.aa, sep= &quot;_&quot;)


 df
  CDR3.light.aa            CDR3.heavy.aa                                  a
1            NA CAGDLSVGGVLAGGVPHLRHFDPW        CAGDLSVGGVLAGGVPHLRHFDPW_NA
2            NA                       NA                              NA_NA
3 CSSYAGSNTFGLF     CPETPRYCSGGFCYGEFDNW CPETPRYCSGGFCYGEFDNW_CSSYAGSNTFGLF
4  CSSYAGSNTGLF                       NA                    NA_CSSYAGSNTGLF
</code></pre>

CC BY-SA 4.0
2023-06-05T10:22:25.740Z
2023-06-05T10:22:25.740Z
Basti
<p>I have antibody data the first column is heavy chain sequence and the second column is the light chain</p>
<p>I want to create a third column by combining two columns like</p>
<p>If <code>CARGRGIAVAGTTGFDYW</code> is a heavy chain and <code>CQQYNSYSLTF</code> a light chain, in third column I want <code>CARGRGIAVAGTTGFDYW_CQQYNSYSLTF</code></p>
<p>if a heavy or light chain is absent, I want something like <code>NA_CQQYNSYSLTF</code> or <code>CARGRGIAVAGTTGFDYW_NA</code> depends of absent part</p>
<pre><code>CDR3.light.aa   CDR3.heavy.aa
-              CAGDLSVGGVLAGGVPHLRHFDPW
-              CPETPRYCSGGFCYGEFDNW

CSSYAGSNTFGLF   -
CSSYAGSNTGLF    -
</code></pre>
<p>So far I have done this</p>
<pre><code>a<span class="math-container">$a &lt;- paste(a$</span>CDR3.heavy.aa, a$CDR3.light.aa, sep= &quot;_&quot;)
</code></pre>
<p>But I noticed a lot of mess and manual works</p>
<p>Any help please?</p>

CC BY-SA 4.0
2023-06-04T20:22:10.390Z
What did you try so far?
user438383
CC BY-SA 4.0
2023-06-05T09:50:39.997Z
Thank you, I have added my code in the post
Zizogolu
<r>
Make a new column in R or bash
# Make a new column in R or bash

(Asked by: username_0 on 2023-06-04)

I have antibody data the first column is heavy chain sequence and the second column is the light chain
I want to create a third column by combining two columns like
If CARGRGIAVAGTTGFDYW is a heavy chain and CQQYNSYSLTF a light chain, in third column I want CARGRGIAVAGTTGFDYW_CQQYNSYSLTF
if a heavy or light chain is absent, I want something like NA_CQQYNSYSLTF or CARGRGIAVAGTTGFDYW_NA depends of absent part
CDR3.light.aa   CDR3.heavy.aa
-              CAGDLSVGGVLAGGVPHLRHFDPW
-              CPETPRYCSGGFCYGEFDNW

CSSYAGSNTFGLF   -
CSSYAGSNTGLF    -

So far I have done this
a$a <- paste(a$CDR3.heavy.aa, a$CDR3.light.aa, sep= "_")

But I noticed a lot of mess and manual works
Any help please?

**Tags:** r
> **Comments:**
> **username_1**: What did you try so far?  
> **username_0**: Thank you, I have added my code in the post  

---

## 1 answers

### Answer by username_2 on 2023-06-05. Score: 3 | Accepted answer

Your approach was correct, you just have to transform your dataframe first to replace the character "-" by NA :
library(stringr)
library(dplyr)
df=data.frame(CDR3.light.aa=c("-","-","CSSYAGSNTFGLF",'CSSYAGSNTGLF'),
              CDR3.heavy.aa=c("CAGDLSVGGVLAGGVPHLRHFDPW","-","CPETPRYCSGGFCYGEFDNW","-"))

df=df %>% 
  mutate_all(funs(str_replace(., "-", "NA")))      


df$a <- paste(df$CDR3.heavy.aa, df$CDR3.light.aa, sep= "_")


 df
  CDR3.light.aa            CDR3.heavy.aa                                  a
1            NA CAGDLSVGGVLAGGVPHLRHFDPW        CAGDLSVGGVLAGGVPHLRHFDPW_NA
2            NA                       NA                              NA_NA
3 CSSYAGSNTFGLF     CPETPRYCSGGFCYGEFDNW CPETPRYCSGGFCYGEFDNW_CSSYAGSNTFGLF
4  CSSYAGSNTGLF                       NA                    NA_CSSYAGSNTGLF


---

