Count common words in two strings

后端 未结 3 2056
春和景丽
春和景丽 2020-12-10 19:00

I have two strings:

a <- \"Roy lives in Japan and travels to Africa\"
b <- \"Roy travels Africa with this wife\"

I am looking to get

3条回答
  •  春和景丽
    2020-12-10 19:13

    You can use strsplit and intersect from the base library:

    > a <- "Roy lives in Japan and travels to Africa"
    > b <- "Roy travels Africa with this wife"
    > a_split <- unlist(strsplit(a, sep=" "))
    > b_split <- unlist(strsplit(b, sep=" "))
    > length(intersect(a_split, b_split))
    [1] 3
    

提交回复
热议问题