我创建了一个数据列表,如下所示
> 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]]?
如果我正确理解
lapply(sites, function(x) x[order(x$Depth_m),])
会给你想要的。