Having a list of vector of strings:
xx <- c("concord wanderer basic set air snug beige",
"concord wanderer basic set air snug black noir",
"concord wanderer basic set air snug blue bleu",
"concord wanderer basic set air snug brown marron",
"concord wanderer basic set air snug green vert",
"concord wanderer basic set air snug grey gris",
"concord wanderer basic set air snug red rouge",
"concord wanderer basic set air snug rose" )
I tried to get minimal shared part between elements of the vector, for example, here I should get:
"concord wanderer basic set air snug"
xx is a result of a previous process, so I am sure that there is a shared part between the elements. But the removed part is not always at the end of he strings.
Using strsplit
and `table
I get this partial solution, but it is a little bit tricky and I loose the original order of words:
table_x <- table(unlist(strsplit(xx,' ')))
paste(names(table_x[table_x==max(table_x)]),collapse=' ')
[1] "air basic concord set snug wanderer"
I am pretty sure that there is better solution. I tried with agrep
or adist
but without a lot of success.
You could use intersect
with Reduce
to get the output you want.
paste(Reduce(intersect, strsplit(xx, " ")), collapse=" ")
#[1] "concord wanderer basic set air snug"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With