rdata-wrangling

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

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)