# Your data
A <- data.frame(pattern = c("ab", "be|eb", "cc", "dd"),
ref = c("first", "second", "third", "fourth"), stringsAsFactors = F)
B <- data.frame(name = c("aa1", "bb1", "cab", "ccaa", "abed" ,"ddd", "ebba"), stringsAsFactors = F)
patternfind <- function(i){
ifelse(grepl(A$pattern[[i]], B$name), A$ref[[i]], NA)
} # grepl function for your apply
m = sapply(seq_along(A$pattern), patternfind) # apply function
test <- cbind(B,m) #bind your pattern matrix to B
melt(test, id = c("name"), value.name = "new", na.rm = T) # melt data for output
name variable new
3 cab 1 first
5 abed 1 first
12 abed 2 second
14 ebba 2 second
18 ccaa 3 third
27 ddd 4 fourth
Если вы хотите пойти по маршруту data.table
.
library(data.table)
DT.A <- as.data.table(A) # set as data tables
DT.B <- as.data.table(B)
ab <- DT.A[, DT.B[grep(pattern, name)], by=.(pattern, new = ref)] # use grep and by, leave out pattern if don't need to see what matched
ab[,c(3,2,1)] # reorder to your desired order
ab[,3:2] # subset to remove the pattern if you decide you don't want to display it
name new
1: cab first
2: abed first
3: abed second
4: ebba second
5: ccaa third
6: ddd fourth