Search code examples
dplyrtidyverse

How to update variable value according another dataframe,just like update function in SQL


How to update dataframe DF_A ,according DF_B ? The result as DF_A UPDATE

library(tidyverse)
DF_A <- data.frame(CAT=c("A","B","C","D","E","F","G"))


DF_B <-data.frame(CAT=c("A","C","F"),
CAT_NEW=c("XZ","TF","ZE"))


DF_A_update <- data.frame(CAT=c("XZ","B","TF","D","E","ZE","G"))

enter image description here


Solution

  • you can try to join both data.frames and replace the values in CAT, e.g.:

    DF_A |> 
      dplyr::left_join(DF_B, by = "CAT") |> 
      dplyr::mutate(CAT = dplyr::if_else(is.na(CAT_NEW), CAT, CAT_NEW)) |> 
      dplyr::select(CAT) -> DF_NEW
    
    identical(DF_NEW, DF_A_update)