uller
uller

Reputation: 115

List within list and lapply

I have a list with ten elements and each of these is a list with 2 elements. This is part of what I get with str():

> str(results)
List of 10
 $ :List of 2

I want to apply the function min to the vector formed by the second element of each list, but I have no idea of how to do it with nested lists.

Example

results <- list(list(1, 2), list(3, 4), list(5, 6), list(4, 1), list(5, 1),
                list(5, 1), list(-1, 10), list(6, 0), list(0, 9), list(4, 4))

Upvotes: 3

Views: 1441

Answers (1)

akuiper
akuiper

Reputation: 214927

This might help:

min(sapply(results, `[[`, 2))

or equivalently:

min(sapply(results, function(x) { x[[2]] }))

Upvotes: 3

Related Questions