Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Filling in a column in a dataframe using another dataframe that partially matches

Using R, I am trying to partially fill in a dataframe (~200 rows) using another (~170) rows by matching on an ID variable. Roughly 50% of the IDs match, and I’d like to just leave the other values blank.

Example:

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enter image description here

What I would like to create is this:

enter image description here

I’ve been trying to sort out a way to do this but am stuck. Using the ‘merge’ function eliminates any row that doesn’t have a corresponding value in the other dataframe but I’d like to preserve all IDs from DF1.

Thank you in advance!

>Solution :

You can use all.x with merge:

df1 = data.frame(ID = c("AA","AB","AC","AD","AE","AF"))
df2 = data.frame(ID = c("AA","BA","BB","AC"), Var1 = c(1,1,2,2), Var2 = c(2,2,2,2))

merge(x = df1, y = df2, by = "ID", all.x = TRUE)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading