Find common substrings between two character variables
Here’s a CRAN package for that: library(qualV) sapply(seq_along(a), function(i) paste(LCS(strsplit(a[i], ”)[[1]], strsplit(b[i], ”)[[1]])$LCS, collapse = “”))
Here’s a CRAN package for that: library(qualV) sapply(seq_along(a), function(i) paste(LCS(strsplit(a[i], ”)[[1]], strsplit(b[i], ”)[[1]])$LCS, collapse = “”))
Here are three possible solutions. library(stringi) library(stringdist) a <- “hello” b <- “hel123l5678o” ## get all forward substrings of ‘b’ sb <- stri_sub(b, 1, 1:nchar(b)) ## extract them from ‘a’ if they exist sstr <- na.omit(stri_extract_all_coll(a, sb, simplify=TRUE)) ## match the longest one sstr[which.max(nchar(sstr))] # [1] “hel” There are also adist() and agrep() in base … Read more