I have a vector of words:
str <- c("The", "Cat", "Jumped")
I would like to find all combinations of words and insert a "+" between any combinations of more than one, similar to:
paste(str, collapse = " + ")
# [1] "The + Cat + Jumped"
My desired output would be:
want <- c("The", "Cat", "Jumped",
"The + Cat", "The + Jumped",
"Cat + Jumped",
"The + Cat + Jumped")
Also note I need only combinations so order doesn't matter and either "The + Cat"
or "Cat + The"
is fine, but I do not want both.
I tried some approaches with combn
(here), outer
(here), expand.grid
(here), and following @Akrun's suggestion on a similar question r - Get different combinations of words but to no avail.
unlist(lapply(seq(str), combn, x=str, paste, collapse=' + '))
[1] "The" "Cat" "Jumped" "The + Cat" "The + Jumped"
[6] "Cat + Jumped" "The + Cat + Jumped"