1

I'm trying to combine these 2 tables into 1 dataframe but the issue is that the Patient Numbers don't match up.

This is roughly the first table (there's many more patients but this is the overall format)

Patient BR
Patient1 PD
Patient10 PR
Patient100 PR
Patient102 MR
Patient104 PR
Patient105 MR
Patient106 CR
Patient107 PR
Patient108 PD
Patient11 PD

This is the second table, which has the RNA-Seq Genes (there's many more genes, but general format)

A1BG A1BG.AS1
Patient100 19.6142415 0.03518250
Patient102 20.0754271 0.20839544
Patient105 13.0091690 0.00000000
Patient106 18.3631301 0.00000000
Patient107 0.0000000 0.00000000
Patient108 0.6060426 0.00000000
Patient10 10.0499299 0.00000000
Patient112 17.7827197 0.11622693
Patient116 16.1881696 0.09452946
Patient117 16.4916958 0.04433252

My question is How do I merge these 2 into 1 table or dataframe, so that the patients will align and their corresponding values?

Martin Gal
  • 16,640
  • 5
  • 21
  • 39
ww22an
  • 41
  • 2
  • 6
  • 1
    What do you mean by the patientIDs not lining up? Can you post a reproducible example? https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Bill O'Brien Jul 20 '21 at 19:28
  • 1
    Does your second data.frame use row names or did you omit the patients column names? Nevertheless: you need `merge`. – Martin Gal Jul 20 '21 at 19:31
  • 1
    Please follow @BillO'Brien's advice in producing example data when you ask questions in the future. Are the patients ID unique in the two tables? If there are duplicate entries how do you want to resolve that when merging? – Yuan Yin Jul 20 '21 at 19:34
  • Thanks, will do reproducible in the future - They're the same Patient IDs, just sorted in a different order as the table shows. @Martin Gal the data table doesn't have the Patient header for the column, is it necessary to add that to merge? – ww22an Jul 20 '21 at 19:45
  • 1
    You need to identify the column (see @RoryS answer) to do the merge. – Martin Gal Jul 20 '21 at 19:47

1 Answers1

2

Assuming your tables are called table1 and table2 respectively and share the common column Patient, use left_join from tidyverse, e.g.:

library(tidyverse)
left_join(table1, table2, by = "Patient")
Rory S
  • 1,278
  • 5
  • 17