本文介绍了在 R 中生成列表的所有不同排列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试创建一个列表的排列列表,例如, perms(list("a", "b", "c")) 返回
I'm trying to create a list of permutations of a list, such that, for example, perms(list("a", "b", "c")) returns
list(list("a", "b", "c"), list("a", "c", "b"), list("b", "a", "c"), list("b", "c", "a"), list("c", "a", "b"), list("c", "b", "a"))我不确定如何进行,任何帮助将不胜感激.
I'm not sure how to proceed, any help would be greatly appreciated.
推荐答案combinat::permn 将完成这项工作:
> library(combinat) > permn(letters[1:3]) [[1]] [1] "a" "b" "c" [[2]] [1] "a" "c" "b" [[3]] [1] "c" "a" "b" [[4]] [1] "c" "b" "a" [[5]] [1] "b" "c" "a" [[6]] [1] "b" "a" "c"请注意,如果元素很大,则计算量很大.
Note that calculation is huge if the element is large.
更多推荐
在 R 中生成列表的所有不同排列
发布评论