简体   繁体   中英

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

I have 2 dataframes (df1 & df2)

df 1:

在此处输入图像描述

df 2:

在此处输入图像描述

Required Output:

df3

在此处输入图像描述

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM