Suppose there is a list as follows:
x <- list(a = list(a1 = 1, a2 = 2), b = list(a2 = 3, a1 = 4))
The positions/names are mixed in the sublists, and to pluck out the a1
s from the list, I would do the following in purrr
.
x %>% map(purrr::pluck, "a1")
$`a`
[1] 1
$b
[1] 4
To throw out an element instead of keeping it, I experimented a bit, and came up with the following (I threw out a2
here).
x %>% map(purrr::assign_in, "a2", value = NULL)
$`a`
$`a`$`a1`
[1] 1
$b
$b$`a1`
[1] 4
In terms of plucking, I actually like the second style better---that is, to keep the list indexing structure as is, while returning only the elements that I want. So I would prefer that once I perform x %>% map(purrr::pluck, "a1")
, I get the second result.
Alternatively maybe there is a better way of throwing objects out in purrr
that I'm not aware of, so that the output styles of the two code (plucking, throwing away) would be consistent?