提问人:confusedindividual 提问时间:11/22/2022 最后编辑:Sotosconfusedindividual 更新时间:11/22/2022 访问量:84
在 R 中从另一个列表创建包含所有可能组合的列表
Create list with all possible combinations from another list in R
问:
在 R 中从另一个列表创建包含所有可能组合的列表
我看到有人问这个问题,但只找到了带有其他软件答案的线程,而不是我 R
基本上,我只想创建一个包含所有可能的独特组合的列表
所需输入
a, b, c
所需输出
a, ab, ac, b, bc, c, abc
答:
3赞
user2974951
11/22/2022
#1
一种可能性
> x=letters[1:3]
> rapply(sapply(seq_along(x),function(y){combn(x,y,simplify=F)}),paste0,collapse="")
[1] "a" "b" "c" "ab" "ac" "bc" "abc"
3赞
ThomasIsCoding
11/22/2022
#2
您可以尝试生成所有这些组合combn
> x <- head(letters, 3)
> unlist(lapply(seq_along(x), combn, x = x, simplify = FALSE), recursive = FALSE)
[[1]]
[1] "a"
[[2]]
[1] "b"
[[3]]
[1] "c"
[[4]]
[1] "a" "b"
[[5]]
[1] "a" "c"
[[6]]
[1] "b" "c"
[[7]]
[1] "a" "b" "c"
或
> unlist(lapply(seq_along(x), function(k) combn(x, k, paste0, collapse = "")))
[1] "a" "b" "c" "ab" "ac" "bc" "abc"
评论