Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Replace value based on match between two dataframes

Let’s say I have two starting data frames:

df1 <- data.frame(code1 = c("a", "b","z"), code2 = c("2", "3", "4"))
df2 <- data.frame(code1 = c("c", "o", "p"), code2 = c("2", "4", "5"), 
                  column3 = "a", column4 = "b", column5 = "c")

I want to match the two data frames by the column ‘code2’ and where that’s a match, replace the value of code1 in df1 to the value of code1 in df2 so that the final data frame looks like this:

df3<- data.frame(code1 = c("c", "b", "o"), code2 = c("2", "3", "4"))

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Here’s a solution with dplyr. It "looks up" code1 in df2, wherever code2 matches; and when no match is found, it defaults to the original code1 in df1.

Solution

library(dplyr)


# ...
# Code to generate 'df1' and 'df2'.
# ...


df1 %>% mutate(code1 = coalesce(
  # Look up the 'code1' according to 'code2'...
  df2$code1[match(code2, df2$code2)],
  # ...and otherwise default to the original 'code1'.
  code1
))

Result

Given df1 and df2 as in your example

df1 <- data.frame(
  code1 = c("a", "b","z"),
  code2 = c("2", "3", "4")
)

df2 <- data.frame(
  code1 = c("c", "o", "p"),
  code2 = c("2", "4", "5"),
  column3 = "a",
  column4 = "b",
  column5 = "c"
)

this solution should yield the desired result:

  code1 code2
1     c     2
2     b     3
3     o     4

Note

One advantage of using match() rather than a dplyr::*_join(): no additional steps are needed to purge extraneous columns from your results.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading