我正在使用 CRAN 包组合中的 permn 函数来生成一个列表列表,该列表由一系列数字的所有排列组成,如下所示
x <- str(permn(3))
x
List of 6
$ : int [1:3] 1 2 3
$ : int [1:3] 1 3 2
$ : int [1:3] 3 1 2
$ : int [1:3] 3 2 1
$ : int [1:3] 2 3 1
$ : int [1:3] 2 1 3
我也有
typeof(x[[1]])
"integer"
我的目标是按数字升序对这个列表列表进行排序(每个元素都需要被视为一个三位数字),而以下工作正常
permn(3)->x
x[order(sapply(x,'[[',1))]
[[1]]
[1] 1 2 3
[[2]]
[1] 1 3 2
...
任何 permn(n) n>3 都会产生错误的排序。我意识到我的问题是我正在对单个值而不是 n 位数字进行排序,但我不知道如何正确分解、排序和重组。我试过了
function(n){
out <-list()
permn(n)->list.of.lists
for(i in 1:length(list.of.lists)){
out[[length(out)+1]] <- unlist(list.of.lists[i])
}
out
但它给出了相同的结果
x[order(sapply(x,'[[',1))]
也就是说,我没有在我正在使用的其他元素的上下文中对每个元素进行排序。
有什么建议么?