首页 > 解决方案 > 按值过滤列表列表

问题描述

我有一个列表列表,例如下面的“输入列表”。我想为所有“cor”> 0.2 过滤它,如下面的输出列表。像这样的嵌套列表对我来说很棘手,因此非常感谢任何提示。

输入列表

$TimeForOrder
            cor  lag
4893 0.09260373 1610

$OrderForPick
           cor lag
3263 0.2926644 -20

$TimeForShip
           cor  lag
2925 0.1249888 -358

$TimeForRelease
           cor lag
3285 0.2335587   2

输出列表

$OrderForPick
           cor lag
3263 0.2926644 -20

$TimeForRelease
           cor lag
3285 0.2335587   2

标签: rlistlapply

解决方案


你可以试试:

Filter(function(x) x$cor >= 0.2, ll)  

#$OrderForPick
#    cor lag
#1 0.292  -2
#
#$TimeForRelease
#    cor lag
#1 0.233   2

还有可能:

ll[vapply(ll, function(x) x$cor >= 0.2, logical(1))]

数据:

TimeForOrder <- data.frame(cor = 0.092, lag = 1610)
OrderForPick <- data.frame(cor = 0.292, lag = -2)
TimeForShip  <- data.frame(cor = 0.124, lag = -358)
TimeForRelease <- data.frame(cor = 0.233, lag = 2)

ll <- list(TimeForOrder = TimeForOrder, OrderForPick = OrderForPick, TimeForShip = TimeForShip, TimeForRelease= TimeForRelease)

推荐阅读