R语言 获取长度为N [duplicate]的字符串的所有组合

j0pj023g  于 2023-04-03  发布在  其他
关注(0)|答案(2)|浏览(126)

此问题在此处已有答案

Unordered combinations of all lengths(3个答案)
3天前关闭。
我有以下数据:

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

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

我试图获得变量/字符的所有可能组合,其中长度可以是1,2,3,4等。
预期产出:

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

oranges
oranges + pears
oranges + pears + bananas

pears
pears + bananas
qgelzfjb

qgelzfjb1#

我们可以使用'rje'包中的powerSet函数,并将输出 Package 在lapply()中,以创建公式对象或字符串的列表:

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"

创建于2023年3月30日,使用reprex v2.0.2

disho6za

disho6za2#

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")
  }
}

内部循环遍历从i到ncol(fruits_split)的所有列,确保生成所有可能的fruits组合,直到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")
  }
}

输出:

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

相关问题