Search code examples
rtidyversetidytable

Updating rows of a data.frame with the rows of another data.frame


I want to update the rows of data.frame df1 with the rows of data.frame df2. Any hint?

df1 <-
  data.frame(
    "V1" = LETTERS[1:4]
  , "V2" = 1:4
  , "V3" = 7:10
  )

df1
  V1 V2 V3
1  A  1  7
2  B  2  8
3  C  3  9
4  D  4 10


df2 <-
  data.frame(
      "V1" = c("A","D")
    , "V2" = c(5, 7)
    , "V3" = c(12, 15)
  )


df2
  V1 V2 V3
1  A  5 12
2  D  7 15

Required Output

  V1 V2 V3
1  A  5 12
2  B  2  8
3  C  3  9
4  D  7 15

Solution

  • use dplyr 1.0.0

    rows_update(df1, df2)
    
    Matching, by = "V1"
      V1 V2 V3
    1  A  5 12
    2  B  2  8
    3  C  3  9
    4  D  7 15