1

I have the following data:

fruits <- c(
  "apples and oranges and pears and bananas",
  "pineapples and mangos and guavas"
)


str_split(fruits, " and ", simplify = TRUE)

I am trying to obtain all possible combinations of the variables/characters where the length can be 1, 2, 3, 4 etc.

Expected output:

apples
apples + oranges
apples + oranges + pears
apples + oranges + pears + bananas

oranges
oranges + pears
oranges + pears + bananas

pears
pears + bananas
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81
user113156
  • 6,761
  • 5
  • 35
  • 81
  • 2
    You say "all possible" combinations, but your desired output is missing the following combinations: `"apples" "pears"; "bananas"; "apples" "bananas"; "oranges" "bananas"; "apples" "oranges" "bananas"; "pears" "bananas"; "apples" "pears" "bananas"`. – TimTeaFan Mar 30 '23 at 11:26
  • What is your desired output? A formula object? A list of strings? – TimTeaFan Mar 30 '23 at 11:27

2 Answers2

2
fruits <- c(
  "apples and oranges and pears and bananas",
  "pineapples and mangos and guavas"
)

fruits_split <- str_split(fruits, " and ", simplify = TRUE)

for (i in 1:ncol(fruits_split)) {
  for (j in i:ncol(fruits_split)) {
    cat(paste(fruits_split[i:j], collapse = " + "), "\n")
  }
}

Is this what you are looking for? the inner loop iterates through all columns from i to ncol(fruits_split), ensuring that all possible combinations of fruits are generated up to the total number of fruits.

fruits <- c(
  "apples and oranges and pears and bananas and Pineapples and mangos and guavas"
)

fruits_split <- str_split(fruits, " and ", simplify = TRUE)

for (i in 1:ncol(fruits_split)) {
  for (j in i:ncol(fruits_split)) {
    cat(paste(fruits_split[i:j], collapse = " + "), "\n")
  }
}

output:

apples 
apples + oranges 
apples + oranges + pears 
apples + oranges + pears + bananas 
apples + oranges + pears + bananas + Pineapples 
apples + oranges + pears + bananas + Pineapples + mangos 
apples + oranges + pears + bananas + Pineapples + mangos + guavas 
oranges 
oranges + pears 
oranges + pears + bananas 
oranges + pears + bananas + Pineapples 
oranges + pears + bananas + Pineapples + mangos 
oranges + pears + bananas + Pineapples + mangos + guavas 
pears 
pears + bananas 
pears + bananas + Pineapples 
pears + bananas + Pineapples + mangos 
pears + bananas + Pineapples + mangos + guavas 
bananas 
bananas + Pineapples 
bananas + Pineapples + mangos 
bananas + Pineapples + mangos + guavas 
Pineapples 
Pineapples + mangos 
Pineapples + mangos + guavas 
mangos 
mangos + guavas 
guavas
szmple
  • 453
  • 1
  • 8
2

We can use the powerSet function from the 'rje' package and wrap the output in lapply() to either create a list of formula objects or strings:

library(stringr)
library(rje)

fruits <- c(
  "apples and oranges and pears and bananas",
  "pineapples and mangos and guavas"
)

fruit_ls <- str_split(fruits, " and ") 

# formula objects
lapply(powerSet(fruit_ls[[1]])[-1], \(x) reformulate(x))

#> [[1]]
#> ~apples
#> <environment: 0x0000028529d9f218>
#> 
#> [[2]]
#> ~oranges
#> <environment: 0x0000028529d7dc20>
#> 
#> [[3]]
#> ~apples + oranges
#> <environment: 0x00000285296ceb68>
#> 
#> [[4]]
#> ~pears
#> <environment: 0x00000285296c4338>
#> 
#> [[5]]
#> ~apples + pears
#> <environment: 0x00000285296aeeb8>
#> 
#> [[6]]
#> ~oranges + pears
#> <environment: 0x000002852969cf78>
#> 
#> [[7]]
#> ~apples + oranges + pears
#> <environment: 0x00000285296979b0>
#> 
#> [[8]]
#> ~bananas
#> <environment: 0x0000028529685808>
#> 
#> [[9]]
#> ~apples + bananas
#> <environment: 0x0000028529694870>
#> 
#> [[10]]
#> ~oranges + bananas
#> <environment: 0x0000028529681468>
#> 
#> [[11]]
#> ~apples + oranges + bananas
#> <environment: 0x000002852968f2e0>
#> 
#> [[12]]
#> ~pears + bananas
#> <environment: 0x000002852968aee8>
#> 
#> [[13]]
#> ~apples + pears + bananas
#> <environment: 0x000002852966e0b8>
#> 
#> [[14]]
#> ~oranges + pears + bananas
#> <environment: 0x0000028529670950>
#> 
#> [[15]]
#> ~apples + oranges + pears + bananas
#> <environment: 0x0000028529652f98>


# strings
lapply(powerSet(fruit_ls[[1]])[-1], \(x) paste(x, collapse = " + "))

#> [[1]]
#> [1] "apples"
#> 
#> [[2]]
#> [1] "oranges"
#> 
#> [[3]]
#> [1] "apples + oranges"
#> 
#> [[4]]
#> [1] "pears"
#> 
#> [[5]]
#> [1] "apples + pears"
#> 
#> [[6]]
#> [1] "oranges + pears"
#> 
#> [[7]]
#> [1] "apples + oranges + pears"
#> 
#> [[8]]
#> [1] "bananas"
#> 
#> [[9]]
#> [1] "apples + bananas"
#> 
#> [[10]]
#> [1] "oranges + bananas"
#> 
#> [[11]]
#> [1] "apples + oranges + bananas"
#> 
#> [[12]]
#> [1] "pears + bananas"
#> 
#> [[13]]
#> [1] "apples + pears + bananas"
#> 
#> [[14]]
#> [1] "oranges + pears + bananas"
#> 
#> [[15]]
#> [1] "apples + oranges + pears + bananas"

Created on 2023-03-30 with reprex v2.0.2

TimTeaFan
  • 17,549
  • 4
  • 18
  • 39