Seems like a basic question and perhaps I'm just missing something obvious ... but is there any way to pluck
a sublist (with purrr
)?
More specifically, here's an initial list:
l <- list(a = "foo", b = "bar", c = "baz")
And I want to return a new (sub-)list with only elements a
and b
.
Normally, I'd just do 'base' R sub-listing:
l[c("a", "b")]
But this doesn't provide the nice .default
handling of pluck
.
My understanding is that pluck 'replaces' [[
, but is there a purrr
equivalent for replacing [
?
[
exists, at least not inpurrr
. I guess a workaround would bemap(set_names(c("a", "b", "z")), partial(pluck, l), .default = "Not found!")
but that's not very neat! – Holstein