簡體   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