I have a dataframe 'df' that looks like this:
LatName | ComName |
---|---|
Todd Smith | Becky Jones |
Becky Jones | |
Becky Jones | |
Rachel Adams |
And another dataframe 'df2' that looks like this:
LatinName | CommonName |
---|---|
Brad Robbins | Becky Jones |
Steve Reisen | Rachel Adams |
Connor McDougal | Charlie Williams |
If want to match values in ComName and CommonName and if they match fill in LatName with LatinName only if LatName is empty to begin with. If LatName isn't empty, then I want that entry left alone, so that the end result of df looks like this:
LatName | ComName |
---|---|
Todd Smith | Becky Jones |
Brad Robbins | Becky Jones |
Brad Robbins | Becky Jones |
Steve Reisen | Rachel Adams |
I left the blank row in there on purpose because some of my rows have nothing in them.
Thank you!
CodePudding user response:
We could do a join and then coalesce
library(dplyr)
left_join(df1, df2, by = c("ComName" = "CommonName")) %>%
mutate(LatName = coalesce(LatName, LatinName), LatinName = NULL)
-output
LatName ComName
1 Todd Smith Becky Jones
2 Brad Robbins Becky Jones
3 Brad Robbins Becky Jones
4 Steve Reisen Rachel Adams
data
df1 <- structure(list(LatName = c("Todd Smith", NA, NA, NA),
ComName = c("Becky Jones",
"Becky Jones", "Becky Jones", "Rachel Adams")), class = "data.frame",
row.names = c(NA,
-4L))
df2 <- structure(list(LatinName = c("Brad Robbins", "Steve Reisen",
"Connor McDougal"), CommonName = c("Becky Jones", "Rachel Adams",
"Charlie Williams")), class = "data.frame", row.names = c(NA,
-3L))