Rajhesh
Rajhesh

Reputation: 21

R: Is there a way to Vlookup with partial match between two dataframes columns

I have 2 dataframes (df1 & df2)

df 1:

enter image description here

df 2 :

enter image description here

Required Output:

df3

enter image description here

Upvotes: 0

Views: 197

Answers (1)

user1828605
user1828605

Reputation: 1735

Instead of the pictures, you should try to put the data in reproducible format. Also, try to show some work but I can understand that you may not know where to even begin. But at least show what you have attempted whether it works or not.

Will something like this work?

library(dplyr)
df1 <- data.frame(almId = c(12347, 123455, 112625, 112621), almname = c("1001 battery down", "2077 Power issue", "7166 DG fault", "2122 cable cut"))

df2 <- data.frame(almname = c("battery down", "Power issue", "DG fault", "Circuit break"))

k <- paste0(df2$almname, collapse = "|")
df1 %>% mutate(Stat = stringr::str_detect(.$almname, k))

df3 <- df1 %>% mutate(Stat = stringr::str_detect(.$almname, k))

   almId           almname  Stat
1  12347 1001 battery down  TRUE
2 123455 2077 Power issue  TRUE
3 112625 7166 DG fault  TRUE
4 112621 2122 cable cut FALSE

Upvotes: 2

Related Questions