Search code examples
rmergeleft-joinmutate

R Replace column values in dataframe base on matching indexing column in separate dataframe


I have a dataframe 'df1' that looks like:

Number Variable1 Variable Variable3
1 A B C
2 A B C
3 A B C
4 A B C
5 A B C

And I have a second dataframe 'df2' that looks like:

Number Variable1 Variable Variable3
1 D E F
2 G H I
3 J K L
4 M N O
15 P Q R

I want to update the three Variable columns in df1 with the data in the Variable columns in df2 based on matching values in Number so that df1 ends up looking like:

Number Variable1 Variable Variable3
1 D E F
2 G H I
3 J K L
4 M N O
5 A B C

Solution

  • You could use a power_left_join from powerjoin package with conflict = coalesce_yx like this:

    library(powerjoin)
    power_left_join(df1, df2, by = "Number", conflict = coalesce_yx)
    #>   Number Variable1 Variable Variable3
    #> 1      1         D        E         F
    #> 2      2         G        H         I
    #> 3      3         J        K         L
    #> 4      4         M        N         O
    #> 5      5         A        B         C
    

    Created on 2022-12-13 with reprex v2.0.2


    Data:

    df1 <- read.table(text = 'Number    Variable1   Variable    Variable3
    1   A   B   C
    2   A   B   C
    3   A   B   C
    4   A   B   C
    5   A   B   C
    ', header = TRUE)
    
    df2 <- read.table(text = 'Number    Variable1   Variable    Variable3
    1   D   E   F
    2   G   H   I
    3   J   K   L
    4   M   N   O
    15  P   Q   R
    ', header = TRUE)