Extract characters that differ between two strings

后端 未结 6 1959
小蘑菇
小蘑菇 2020-12-03 08:09

I have used adist to calculate the number of characters that differ between two strings:

a <- \"Happy day\"
b <- \"Tappy Pay\"
adist(a,b)          


        
6条回答
  •  青春惊慌失措
    2020-12-03 08:52

    The following function could be a better option to solve problem like this.

    list.string.diff <- function(a, b, exclude = c("-", "?"), ignore.case = TRUE, show.excluded = FALSE)
    {
    if(nchar(a)!=nchar(b)) stop("Lengths of input strings differ. Please check your input.")
    if(ignore.case)
    {
    a <- toupper(a)
    b <- toupper(b)
    }
    split_seqs <- strsplit(c(a, b), split = "")
    only.diff <- (split_seqs[[1]] != split_seqs[[2]])
    only.diff[
    (split_seqs[[1]] %in% exclude) |
    (split_seqs[[2]] %in% exclude)
    ] <- NA
    diff.info<-data.frame(which(is.na(only.diff)|only.diff),
    split_seqs[[1]][only.diff],split_seqs[[2]][only.diff])
    names(diff.info)<-c("position","poly.seq.a","poly.seq.b")
    if(!show.excluded) diff.info<-na.omit(diff.info)
    diff.info
    

    from https://www.r-bloggers.com/extract-different-characters-between-two-strings-of-equal-length/

    Then you can run

    list.string.diff(a, b)
    

    to get the difference.

提交回复
热议问题