繁体   English   中英

使用 R 中另一个列表的所有可能组合创建列表

[英]Create list with all possible combinations from another list in R

使用 R 中另一个列表的所有可能组合创建列表

我看到有人问这个问题,但只找到了其他软件的答案,而不是我 R

基本上我只想创建一个包含所有可能的唯一组合的列表

所需的输入

a, b, c 

希望 output

a, ab, ac, b, bc, c, abc 

一种可能

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

你可以尝试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"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM