R语言 查找向量中的所有单词组合

eagi6jfj  于 2023-10-13  发布在  其他
关注(0)|答案(5)|浏览(124)

我有一个向量的话:

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,但无济于事。

b4qexyjb

b4qexyjb1#

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

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

fdbelqdn2#

您可以对所有可能的大小使用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"
13z8s7eq

13z8s7eq3#

你可以像下面这样用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"
q43xntqr

q43xntqr4#

下面是@MrFlick代码的tidyverse变体:

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"
bd1hkmkf

bd1hkmkf5#

使用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。

相关问题