-3
$\begingroup$

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?

$\endgroup$
2
  • 1
    $\begingroup$ What did you try so far? $\endgroup$
    – user438383
    Jun 4, 2023 at 20:22
  • $\begingroup$ Thank you, I have added my code in the post $\endgroup$
    – Zizogolu
    Jun 5, 2023 at 9:50

1 Answer 1

2
$\begingroup$

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
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.