With base R, you can use match to pair the names together then we can get the ID for those to replace for each column:
df1$Source <- df2$ID[match(df1$Source, df2$Name)]
df1$Target <- df2$ID[match(df1$Target, df2$Name)]
Output
Source Target
1 2678 338
2 6049 8323
3 9873 824
4 3014 1272
5 4055 1272
Another option would be to mutate across and use match again:
library(tidyverse)
df1 %>%
mutate(across(everything(), ~ df2$ID[match(.x, df2$Name)]))
Another option would be to pivot to long form, then join the data together then pivot back wide (but not very efficient).
df1 %>%
pivot_longer(everything()) %>%
left_join(., df2, by = c("value" = "Name")) %>%
select(-value) %>%
group_by(grp = ceiling(row_number()/2)) %>%
pivot_wider(names_from = "name", values_from = "ID") %>%
select(-grp)
Data
df1 <- structure(list(Source = c("DORTMUND", "MUMBAI", "XIOALAN", "ETTRINGEN",
"HILTER"), Target = c("ANTWERP", "SPIJKENISSE", "BEILUN", "BREMERHAVEN",
"BREMERHAVEN")), class = "data.frame", row.names = c(NA, -5L))
df2 <- structure(list(ID = c(2678L, 6049L, 9873L, 3014L, 4055L, 338L,
8323L, 824L, 1272L), Name = c("DORTMUND", "MUMBAI", "XIOALAN",
"ETTRINGEN", "HILTER", "ANTWERP", "SPIJKENISSE", "BEILUN", "BREMERHAVEN"
)), class = "data.frame", row.names = c(NA, -9L))