2017-03-16 34 views
3

我有以下列表:順序列表,命令不工作

MYLIST

[[1]] 
[1] 11 
[[2]] 
[1] 9 
[[3]] 
[1] 
10 

我想對它進行排序。 我試圖

sort(mylist) 

Error: mylist must be atomic

sort.list(mylist) 

Error in sort.list(mylist) : 'x' must be atomic for 'sort.list' Have you called 'sort' on a list?

order(mylist) 

Error in order(mylist) : unimplemented type 'list' in 'orderVector1'

回答

3

我們需要ordervector。在這裏,假設list有1只長的每個元素,unlist的「MYLIST」,ordervector,然後使用該

mylist[order(unlist(mylist))] 
#[[1]] 
#[1] 9 

#[[2]] 
#[1] 10 

#[[3]] 
#[1] 11 
+1

解決使用名單的問題。謝謝 – Thegamer23

2

purrr包可能是此任務矯枉過正,但它是值得探索如果您正在尋找一個工具包來處理列表:

library(purrr) 
mylist %>% sort_by(sort) 
+0

謝謝,也許我會在將來檢查 – Thegamer23

1

您的列表只包含每個條目的一個值。所以,你也可以將它轉換爲矢量首先用do.call(c,...)然後排序向量:

sort(do.call(c,mylist))

#[1] 3 10 20 
+0

是的,我知道這個解決方案,但我試圖檢查是否有一個列表的方式(只是爲了好奇)。因爲有人不知道可能會覺得它有用,所以我投票選舉 – Thegamer23