0

I have 2 data frames with 2 columns each like

df_prefix:

MacroNode_Prefix                                         Prefix
AAAAATTCGA                                                 ATG
TGACGGCTAT                                                 C
GCATCTTAAC                                                 GC

df_suffix

 MacroNode_Suffix                                         Suffix
  AAAAATTCGA                                                G
  CGAATCATTG                                              ATGGAC
  GCATCTTAAC                                               TCG

I want to create a third data frame where the common nodes from both the data frames will be there along with the respective prefix and suffix. Like:

Prefix                       Common_Node                              Suffix
ATG                          AAAAATTCGA                                 G
GC                           GCATCTTAAC                                TCG
Ashi
  • 61
  • 5
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Jul 23 '20 at 04:49
  • Hi sorry for that. i have edited the question and provided the sample. Let me know if I can give any other details that you might require. – Ashi Jul 23 '20 at 04:59
  • Seeing your expected results, I think you wanna use `inner_join()`: `inner_join(df_prefix, df_suffix, by = c("MacroNode_Prefix" = "MacroNode_Suffix")) %>% rename(common_node = MacroNode_Prefix)` – jazzurro Jul 23 '20 at 05:43

1 Answers1

0

Maybe not the most elegant way, but enough

library(dplyr)
bind_cols(df_prefix, df_suffix) %>% select(Prefix, `MacroNode_Prefix`, Suffix) %>% 
rename(Common_Node = `MacroNode_Prefix`)

# A tibble: 3 x 3
Prefix Common_Node Suffix
 <chr>  <chr>       <chr> 
1 ABC    Bla-bla     XYZ   
2 EFD    Bla-bla     KLM   
3 HIJ    Bla-bla     OPR
Anakin Skywalker
  • 2,400
  • 5
  • 35
  • 63
  • Hi Thanks for the answers. I tried this and I am getting an error. Actually both the data frames are of different size. Maybe I should have mentioned that earlier. The df_pref has 435 rows and df_suff has 441 rows. – Ashi Jul 23 '20 at 05:43
  • You either need to `join` dataframes and drop something or `cbind` something like this or similar https://stackoverflow.com/questions/40399229/cbind-2-dataframes-with-different-number-of-rows – Anakin Skywalker Jul 23 '20 at 05:46
  • Thank you @Anankin Skywalker for the link. I have used the merge function. – Ashi Jul 23 '20 at 06:52