I've a string with a list of words, and I want to get all the possible word combinations from it.
fruits <- "Apple Banana Cherry"
To get this output:
"Apple, Banana, Cherry, Apple Banana, Apple Cherry, Banana Cherry, Apple Banana Cherry"
Using the function defined here, slightly modified:
f1 <- function(str1){
v1 <- strsplit(str1, ' ')[[1]]
paste(unlist(sapply(seq(length(v1)), function(i)
apply(combn(v1, i), 2, paste, collapse=" "))), collapse= ', ')
}
f1(fruits)
This works fine when there's relatively few rows, but the real-life example has a total of 93,300 characters across 3,350 rows with an median string length of 25 characters, causing an error similar to this:
Error in paste(unlist(sapply(seq(length(v1)), function(i) apply(combn(v1, : result would exceed 2^31-1 bytes
I tried changing utils::combn
to RcppAlgos::comboGeneral
within the function , because it's apparently quicker, but still encountered the same issue. Any suggestions for ways around this?