I have 2 data sets, say A and B, I want to add a column to A that has Y or N on weather A$4 matches B$5 and or B$6.
I tried
A %>%
add_column(Match = ifelse(A$4 %in% c(B$5, B$6), Y, N))
However B$5 and B$6 sometimes contain multiple options, separated by a comma.
For example, if a value in A$4 is (CR2), and a value in B$5 is (CR6) and B$6 is (CR1,CR2), I still want the A$Match to be Y. And using the %in% operator only gives Y for an exact match.
I ended up getting some help from a collegue, since the datasets arent too big we opted to loop through the data.
Match_list <- c()
for (i in 1:nrow(file_A)) {
print(file_A[i,4])
if (any(mapply(grepl, file_A[i,4], fileB$5))) {
Match_list <- append(Match_list, "Y")
} else if (any(mapply(grepl, file_A[i,4], fileB$6))) {
Match_list <- append(Match_list, "Y")
} else {
Match_list <- append(Match_list, "N")
}
}
file_A['Match'] <- Match_list