Create a flexible key for merge in R

37 Views Asked by At

I need to perform a merge between two databases but, due to numerous typing errors, I would like to make the criteria for this merge more flexible so that in cases where the keys differ by just one character and there is a single correspondence between the keys 1 and 2, the merge is still performed. For example, in the following dataframes, I would expect the keys AA_DLA_25051946 and BA_DLA_25051946 to still result in a merge between the dataframes.

Chave1<-c("AA_DLA_25051946","BAJ_FRR_10091975","CBR_ARM_30111961")
Chave2<-c("BA_DLA_25051946","AB_FB_31101952","ABR_ARM_30111961")

I created a comparison function that seems to work, but I can't apply it to the merge:

comparar_strings <- function(coluna1, coluna2) {
  # Inicializa um vetor de resultados
  resultados <- logical(length = length(coluna1))
  
  # Loop pelos elementos das colunas
  for (i in seq_along(coluna1)) {
    str1 <- coluna1[i]
    str2 <- coluna2[i]
    
    # Comparar se as strings são idênticas ou diferem por apenas um caractere
    resultados[i] <- sum(strsplit(str1, "")[[1]] != strsplit(str2, "")[[1]]) <= 1
  }
  
  # Retorna vetor de resultados
  return(resultados)
}
0

There are 0 best solutions below