user3476463
user3476463

Reputation: 4575

filter list of list by value

I have a list of lists like the “inputlist” below. I would like to filter it for all “cor” > 0.2 like the outputlist below. Nested lists like this are tricky for me, so any tips are greatly appreciated.

inputlist

$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

outputlist

$OrderForPick
           cor lag
3263 0.2926644 -20

$TimeForRelease
           cor lag
3285 0.2335587   2

Upvotes: 2

Views: 122

Answers (1)

Mike H.
Mike H.

Reputation: 14360

You could try:

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

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

Also possibly:

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

Data:

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)

Upvotes: 3

Related Questions