Q:

How to convert a list to a vector of values of one of its fields?

l = list()
l[[1]] = list(); l[[1]]$asdf = 'jkl'; l[[1]]$qwerty = 5
l[[2]] = list(); l[[2]]$asdf = 'zxcv'; l[[2]]$qwerty = 55
l[[3]] = list(); l[[3]]$asdf = 'poiu'; l[[3]]$qwerty = 555

Now I want to somehow extract the qwertys from this list; ie I want to obtain an equivalent of c(5, 55, 555) since the values of the qwerty fields of consecutive elements of this list are 5, 55, 555.
How to obtain this?
(In case I've fallen into XY: what I really want to achieve is to get the element of this list whose qwerty field has greatest value; and I thought one way to obtain this was to extract the qwertys to an intermediate numeric I could then call max on)

A:

We can pluck the "qwerty" elements by looping through the list with map
library(tidyverse)
map_dbl(l, pluck, "qwerty")
#[1]   5  55 555

Or using sapply from base R
sapply(l, `[[`, "qwerty")
#[1]   5  55 555

wrap with max to get the max value
max(sapply(l, `[[`, "qwerty"))
#[1] 555

If it is the element of the list
i1 <- which.max(sapply(l, `[[`, "qwerty"))
l[i1]
#[[1]]
#[[1]]$asdf
#[1] "poiu"

#[[1]]$qwerty
#[1] 555

Or another option is
l %>%
    transpose %>%
    .$qwerty %>% 
    unlist %>% 
    which.max %>% 
    magrittr::extract(l, .)

