我正在尝试查找和匹配两个数据帧(df1
val=c("AAAA","XXXX","BBBB","YYYY","ZZZZ","MMMM","YYYY","CCCC","GGGG")
df1 <- as.data.frame(val)
val=c("AAAA,BBBB","BBBB,CCCC,FFFF","CCCC,DDDD,GGGG,FFFF","GGGG","")
id =c(1,2,3,4,5)
df2 <- as.data.frame(val,id)
如果在df2中找到df1的值,那么我需要df1中新列的id
查看以下代码是否有帮助。
library(tidyverse)
df2 = df2 %>%
mutate(val=str_split(val, ",", n=Inf)) %>%
unnest
df1 = df1 %>%
left_join(df2, by="val")