Let's say I have two dataframes like below, (real dataset has many more rows and cols)
df = data.frame("Worker" = c("JBB","JDD","MB","JBB"),
"Age" = c(4,5,6,4))
df2 = data.frame("Initials" = c("JBB","JDD","MB","JOD"),
"Worker" = c("Joe Bloggs/JBB", "Jane Doe/JDD",
"Mr. Big/MB", "John Doe/JOD"))
I would like to replace the Worker col in df with the Worker col from df2
In the future more workers will be added to both dataframes so it would be nice if there was a quick and easy way to do this rather than manually doing something like this for each set of initials
df$Worker<-paste(gsub("JBB", "Joe Bloggs/JBB", df$Worker, perl=TRUE))
Perhaps a loop or simply some kind of tidyverse::replace solution
I have tried various joins but they don't work for me.
Have also tried
df %>%
mutate(new_Worker = case_when(df$Worker == df2$Initials ~ df2$Worker)
This gives errors too.
/and join.tidyverse::case_whenbut I just can't figure out the logic and how best to implement in an efficient way, have edited question to reflect trying with joinsdf %>% rename(Initials = Worker) %>% left_join(df2)