R语言 向量中所有单词的最终组合

hi3rlvi2  于 2023-03-05  发布在  其他
关注(0)|答案(5)|浏览(101)

我有一个词的向量:

str <- c("The", "Cat", "Jumped")

我想找出所有词语的组合,并在任何多于一个词语的组合之间插入“+”,类似于:

paste(str, collapse = " + ")
# [1] "The + Cat + Jumped"

我期望的输出是:

want <- c("The", "Cat", "Jumped", 
          "The + Cat", "The + Jumped",
          "Cat + Jumped",
          "The + Cat + Jumped")

还请注意,我只需要组合,所以顺序并不重要,"The + Cat""Cat + The"都可以,但我不想两者都要。
我尝试了combnhere)、outerhere)、expand.gridhere)的一些方法,并遵循@Akrun对类似问题r - Get different combinations of words的建议,但没有效果。

bmp9r5qi

bmp9r5qi1#

您可以使用combn表示所有可能的大小,然后自己折叠结果

str <- c("The", "Cat", "Jumped")
Map(function(i) combn(str, i, simplify = FALSE), seq_along(str)) |>
  unlist(recursive=FALSE) |> 
  sapply(paste, collapse=" + ")
# [1] "The"                "Cat"                "Jumped"            
# [4] "The + Cat"          "The + Jumped"       "Cat + Jumped"      
# [7] "The + Cat + Jumped"
z9smfwbn

z9smfwbn2#

您可以像下面这样使用intToBits

lapply(
  1:(2^length(str) - 1),
  function(k) {
    paste0(str[which(intToBits(k) == 1)], collapse = " + ")
  }
)

它给出了

[[1]]
[1] "The"

[[2]]
[1] "Cat"

[[3]]
[1] "The + Cat"

[[4]]
[1] "Jumped"

[[5]]
[1] "The + Jumped"

[[6]]
[1] "Cat + Jumped"

[[7]]
[1] "The + Cat + Jumped"
sg2wtvxw

sg2wtvxw3#

unlist(lapply(seq(str), combn, x=str, paste, collapse=' + '))

[1] "The"                "Cat"                "Jumped"             "The + Cat"          "The + Jumped"      
[6] "Cat + Jumped"       "The + Cat + Jumped"
0g0grzrc

0g0grzrc4#

下面是@MrFlick代码的变体:

library(tidyverse)

str <- c("The", "Cat", "Jumped")

map(1:length(str), function(i) {
  combn(str, i, simplify = FALSE) %>%
    map(~paste(., collapse = " + "))
  }) %>%
  unlist(recursive = FALSE) %>% 
  unlist()
[1] "The"                "Cat"               
[3] "Jumped"             "The + Cat"         
[5] "The + Jumped"       "Cat + Jumped"      
[7] "The + Cat + Jumped"
szqfcxe2

szqfcxe25#

使用rje包中的powerSet函数:

lapply(rje::powerSet(str), paste, collapse = " + ")
#> [[1]]
#> [1] ""
#> 
#> [[2]]
#> [1] "The"
#> 
#> [[3]]
#> [1] "Cat"
#> 
#> [[4]]
#> [1] "The + Cat"
#> 
#> [[5]]
#> [1] "Jumped"
#> 
#> [[6]]
#> [1] "The + Jumped"
#> 
#> [[7]]
#> [1] "Cat + Jumped"
#> 
#> [[8]]
#> [1] "The + Cat + Jumped"

注意:第一个元素对应于空集,更多信息请参见Power set的wiki。

相关问题