我创建了一个数据列表,如下所示
> sites
[[1]]
ANLL_ID lat long X Y Depth_m
15 B3 23.78038 90.63857 259364 2631912 94.2
16 B4 23.78038 90.63857 259364 2631912 29.9
17 B5 23.78038 90.63857 259364 2631912 47.1
18 B6 23.78038 90.63857 259364 2631912 51.5
19 B7 23.78038 90.63857 259364 2631912 6.0
20 B8 23.78038 90.63857 259364 2631912 10.2
21 B9 23.78038 90.63857 259364 2631912 25.6
[[2]]
ANLL_ID lat long X Y Depth_m
22 C1 23.79 90.611 256572 2633025 15
23 C2 23.79 90.611 256572 2633025 8
24 C3 23.79 90.611 256572 2633025 10
25 C4 23.79 90.611 256572 2633025 94
26 C5 23.79 90.611 256572 2633025 53
... ...
如果我想根据'Depth_m'使用sapply命令/排序(保留所有变量)长列表:
sites.srt<- lapply(1:length(sites),
function(i) sites[order(sites[[i]]$Depth_m),])
但是R会抛出错误 -
Error in sites[order(sites[[i]]$Depth_m), ] :
incorrect number of dimensions
我知道它正在发生,因为数据是LIST,而不是数据帧。我可以把它变成一个数据帧。但是将数据保存为列表使我更容易进行进一步处理。有没有办法在列表中排序 - [[1]],[[2]],...... [[15]]?
答案 0 :(得分:2)
如果我理解正确
lapply(sites, function(x) x[order(x$Depth_m),])
会给你你想要的东西。