我有一个清单

str(overlaps)
List of 7
 $ a5: chr [1:6] "calc_a1c" "predmdx_flag" "bmi" "systolic" ...
 $ a2: chr [1:2] "age" "yr"
 $ a4: chr(0)
 $ a6: chr(0)
 $ a1: chr [1:2] "trig_3cat" "glipizide_flag"
 $ a3: chr [1:2] "email_flag" "statins_flag"
 $ a7: chr [1:4] "trig_3cat.>=200" "antihtn_flag" "black_flag" "gender.M"


我想对列表重新排序,以使结果列表是其中元素按数字顺序排列的列表。即a1,a2,a3等

最佳答案

一些随机数据:

set.seed(42)
overlaps <- replicate(5, runif(3), simplify=FALSE)
names(overlaps) <- paste0("a", sample(5))
str(overlaps)
# List of 5
#  $ a5: num [1:3] 0.915 0.937 0.286
#  $ a4: num [1:3] 0.83 0.642 0.519
#  $ a1: num [1:3] 0.737 0.135 0.657
#  $ a3: num [1:3] 0.705 0.458 0.719
#  $ a2: num [1:3] 0.935 0.255 0.462


排序:

str(overlaps[ sort(names(overlaps)) ])
# List of 5
#  $ a1: num [1:3] 0.737 0.135 0.657
#  $ a2: num [1:3] 0.935 0.255 0.462
#  $ a3: num [1:3] 0.705 0.458 0.719
#  $ a4: num [1:3] 0.83 0.642 0.519
#  $ a5: num [1:3] 0.915 0.937 0.286

关于r - 列表元素的排序,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37979757/

10-17 01:19