You can use sub
to get only the number from DF1$A
and use %in%
to test if the number is present in DF2$A
.
DF1$C <- ""
i <- !sub("\\D*", "", DF1$A) %in% DF2$A
DF1$C[i] <-DF1$A[i]
DF1
# A B C
#1 ABC 1 AA ABC 1
#2 ABC 2 AB
#3 ABC 3 AC
#4 ABC 4 AD
#5 ABC 5 AE ABC 5
#6 ABC 6 AF ABC 6
#7 ABC 7 AG ABC 7
Data:
DF1 <- data.frame(A=paste("ABC", 1:7), B=paste0("A", LETTERS[1:7]))
DF2 <- data.frame(A=c(2:4,9:12), B=paste0("B", LETTERS[1:7]))
solved Compare two DF’s and find differences in a specified column of different data types