Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Base R中按递增顺序重新排列多个列表(而不是它们的元素)_R_List_Function_Loops_Sorting - Fatal编程技术网

如何在Base R中按递增顺序重新排列多个列表(而不是它们的元素)

如何在Base R中按递增顺序重新排列多个列表(而不是它们的元素),r,list,function,loops,sorting,R,List,Function,Loops,Sorting,我想知道如何以递增的方式重新排列3个不同的列表(即,列表彼此不共享任何元素) 例如,在下面的输出中,我希望整个第二个列表占据第一个列表的位置,第三个列表占据第二个列表的位置,依此类推 p.S.我的目标是实现一个函数/循环结构来重新排列任意数量的列表 x = list(20:46, 3:7, 6:9) x[-1] <- Map(setdiff, x[-1], x[-length(x)]) x # output: [[1]] ## FIRST LIST [1] 20 21 22 2

我想知道如何以递增的方式重新排列3个不同的列表(即,列表彼此不共享任何元素)

例如,在下面的输出中,我希望整个第二个列表占据第一个列表的位置,第三个列表占据第二个列表的位置,依此类推

p.S.我的目标是实现一个函数/循环结构来重新排列任意数量的列表

x = list(20:46, 3:7, 6:9)
x[-1] <- Map(setdiff, x[-1], x[-length(x)])
x

# output:

[[1]]    ## FIRST LIST
 [1] 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46

[[2]]    ## SECOND LIST
 [1] 3 4 5 6 7

[[3]]   ## THIRD LIST
 [1] 8 9
x=list(20:46,3:7,6:9)
x[-1]你看:

reorder <- function(x){
 mins <- sapply(x, min) 
 aux=list()
 i=1
 while (length(mins)>0) {
   aux[[i]] <- x[[which.min(mins)]]
   x <- x[-which.min(mins)]
   mins <- mins[-which.min(mins)]
   i=i+1
 }
return(aux)
}

重新排序这些列表总是这样吗?我是说。。。像一个序列

对于这种特殊情况,您只需要从每个列表中选择一个代表性编号,然后您可以将其按如下顺序排列:

x = list(20:46, 3:7, 6:9)
x[-1] <- Map(setdiff, x[-1], x[-length(x)])
x

require(dplyr)
sapply(x, max) %>% order %>% x[.]

[[1]]
[1] 3 4 5 6 7

[[2]]
[1] 8 9

[[3]]
 [1] 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
x=list(20:46,3:7,6:9)
x[-1]%订单%>%x[.]
[[1]]
[1] 3 4 5 6 7
[[2]]
[1] 8 9
[[3]]
[1] 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46

这就是您要查找的内容吗?

列表中的第二个元素和第三个元素彼此共享元素。为什么你需要求助于这个列表?最终目标是什么?谢谢!但是您的函数是否可以修改为对以下内容重新排序:
reorder(list(0:40,0:5))
x = list(20:46, 3:7, 6:9)
x[-1] <- Map(setdiff, x[-1], x[-length(x)])
x

require(dplyr)
sapply(x, max) %>% order %>% x[.]

[[1]]
[1] 3 4 5 6 7

[[2]]
[1] 8 9

[[3]]
 [1] 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46