R: find largest common substring starting at the beginning

后端 未结 11 2696
星月不相逢
星月不相逢 2021-02-19 18:33

I\'ve got 2 vectors:

word1 <- \"bestelling\"   
word2 <- \"bestelbon\"

Now I want to find the largest common substring that starts at the

11条回答
  •  南笙
    南笙 (楼主)
    2021-02-19 19:07

    This will work for an arbitrary vector of words

    words <- c('bestelling', 'bestelbon')
    words.split <- strsplit(words, '')
    words.split <- lapply(words.split, `length<-`, max(nchar(words)))
    words.mat <- do.call(rbind, words.split)
    common.substr.length <- which.max(apply(words.mat, 2, function(col) !length(unique(col)) == 1)) - 1
    substr(words[1], 1, common.substr.length)
    # [1] "bestel"
    

提交回复
热议问题