Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing multiple columns from different dataframe using dplyr

I have two dataframes, one of which contains a subset of IDs and columns of the other (but has different values).

ds1 <- data.frame(id = c(1:4),
                      d1 = "A",
                      d2 = "B",
                      d3 = "C")


ds2 <- data.frame(id = c(1,2),
                     d1 = "W",
                     d2 = "X")

I am hoping to use dplyr on d1 to find the shared columns, and replace their values with those found in d2, matching on ID. I can mutate them one at a time like this:

ds1 %>% 
  mutate(d1 = ifelse(id %in% ds2$id, ds2$d1[ds2$id==id],d1),
         d2 = ifelse(id %in% ds2$id, ds2$d2[ds2$id==id],d2))

In my real situation, I am needing to do this 47 times, however. With the robustness of across(), I feel there is a better way. I am open to non-dplyr solutions as well.

like image 917
FowlPlay Avatar asked Jun 06 '26 07:06

FowlPlay


1 Answers

You may perhaps need this using dplyr and stringr (can be done without stringr also)


library(tidyverse)

ds1 %>% left_join(ds2, by = 'id') %>%
  mutate(across(ends_with('.y'), ~ coalesce(., get(str_replace(cur_column(), '.y', '.x'))))) %>%
  select(!ends_with('.x')) %>%
  rename_with(~str_remove(., '.y'), ends_with('.y'))

#>   id d3 d1 d2
#> 1  1  C  W  X
#> 2  2  C  W  X
#> 3  3  C  A  B
#> 4  4  C  A  B

Created on 2021-05-10 by the reprex package (v2.0.0)

like image 83
AnilGoyal Avatar answered Jun 10 '26 17:06

AnilGoyal