mufernando mufernando - 5 months ago 57
R Question

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.


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))


This might help:

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

or equivalently:

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