Home > Software design >  Search & Extract the string by comparing every value with all values with in the column (even order
Search & Extract the string by comparing every value with all values with in the column (even order

Time:10-05

Hi i have a column and i am trying to compare the 1 value with all other values till the last value and extract the matched string even order of values are different .(for ex : Row no.20 and 24) Output data frame will be df_out.

Col_1 = c("AB,CD,EF","AB,CD,EF,GH","MN,OP","AB,MN,OP","OP,MN,AB")
df_input = data.frame(Col_1)

Output data frame is as follows

Col_1 = c("AB,CD,EF","AB,CD,EF","AB,CD,EF","AB,CD,EF","AB,CD,EF",  "AB,CD,EF,GH","AB,CD,EF,GH","AB,CD,EF,GH","AB,CD,EF,GH","AB,CD,EF,GH","MN,OP","MN,OP","MN,OP","MN,OP","MN,OP", "AB,MN,OP","AB,MN,OP","AB,MN,OP","AB,MN,OP","AB,MN,OP",
          "OP,MN,AB","OP,MN,AB","OP,MN,AB","OP,MN,AB","OP,MN,AB")
Col_2 = c("AB,CD,EF","AB,CD,EF,GH","MN,OP","AB,MN,OP","OP,MN,AB",  "AB,CD,EF","AB,CD,EF,GH","MN,OP","AB,MN,OP","OP,MN,AB",  "AB,CD,EF","AB,CD,EF,GH","MN,OP","AB,MN,OP","OP,MN,AB", "AB,CD,EF","AB,CD,EF,GH","MN,OP","AB,MN,OP","OP,MN,AB",
          "AB,CD,EF","AB,CD,EF,GH","MN,OP","AB,MN,OP","OP,MN,AB")


match = c("Complete Match","AB,CD","NO Matching","AB","AB","AB,CD","Complete Match","NO Matching","AB","AB","NO Matching","NO Matching","Complete Match",
          "MN,OP","MN,OP","AB","AB","MN,OP","Complete Match","Complete Match","AB","AB","MN,OP","Complete Match","Complete Match")


df_out = data.frame(Col_1,Col_2,match)

I have tried with grepl and not able to get the desired output
Thanks in advance

CodePudding user response:

Here is a (somewhat messy) solution:

funcmatch <- function(a, b) {
  ma <- match(a, b)
  if (all(is.na(ma)))
    return("NO MATCH") 
  else if (sum(!is.na(ma)) == length(b)) 
    return("COMPLETE MATCH") 
  else 
    return(paste0(a[na.omit(ma)], collapse = ",")) 
}

mapply(funcmatch, strsplit(Col_1, ","), strsplit(Col_2, ","))

And the solution :

 [1] "COMPLETE MATCH" "AB,CD,EF"       "NO MATCH"      
 [4] "AB"             "EF"             "COMPLETE MATCH"
 [7] "COMPLETE MATCH" "NO MATCH"       "AB"            
[10] "EF"             "NO MATCH"       "NO MATCH"      
[13] "COMPLETE MATCH" "OP,NA"          "OP,MN"         
[16] "AB"             "AB"             "COMPLETE MATCH"
[19] "COMPLETE MATCH" "COMPLETE MATCH" "OP"            
[22] "OP"             "COMPLETE MATCH" "COMPLETE MATCH"
[25] "COMPLETE MATCH"
  • Related