I just read through comparable questions, but found no answering my specific problem. I have two dataframes,
df1 <- data.frame("name" = c("11-24", "Tim", "Anna", "67-14", "A0839", "A4b", "Lisa", "Selina"))
df2 <- data.frame("abbreviation" = c("11-24", "67-14", "A0839", "A4b"),
"name" = c("Charles", "Nick", "Harry", "Lola"))
Looking like this:
> df1
name
1 11-24
2 Tim
3 Anna
4 67-14
5 A0839
6 A4b
7 Lisa
8 Selina
> df2
abbreviation name
1 11-24 Charles
2 67-14 Nick
3 A0839 Harry
4 A4b Lola
I want to replace the abbreviations found in the column "name" of df1 by the matching name in df2. So that 11-24 is replaced by Charles or A4b by Lola.
What I tried was:
df1 <- df1 %>%
mutate(name = ifelse(name %in% df2$abbreviation, df2$name, name))
But this give not the result I want. I want:
> df1
name
1Charles
2 Tim
3 Anna
4 Nick
5 Harry
6 Lola
7 Lisa
8 Selina
My dataframes have a different length. I am looking for a tidyverse-solution, maybe one of you has something in mind..
This would help me a lot :) Best, Kathrin