From the discussion in this twitter thread it seems there is a need to remove elements from lists by name. The current "best" solution is to assign an element NULL using base R commands, which does not have an elegant tidy piping implementation. Since this is a fairly common task that needs to be done, it would be helpful to create a purrr a function that can easily be put within a sequence of piped purrr calls:

One approach I have been thinking of after writing that last tweet would be to create a purrr::keep_names() and purrr::discard_names(). The point of these functions would be to closely mimic the existing purrr::keep() and purrr::discard(), but to have the functions be applied to the names of the list rather than the values. It could also work on a vectors of names as the input rather than a function, for the common case when you just want to keep/remove specific elements. So something like this:
library(purrr)
example <- as.list(1:4)
names(example) <- list("a", "b", "c", "rstudioconf_2022","cat")
> keep_names(example, c("a","b"))
$a
[1] 1
$b
[1] 2
> discard_names(example, ~ .x %in% letters)
$rstudioconf_2022
[1] 3
$cat
[1] 4
And then in the case of the Twitter thread, Elaine could have just added discard_names("b") into her code.
Things I like about adding these functions:
- They solve a problem I personally have as well.
- They seem to open the door a little bit to more functions that work on names that could be useful in purrr. For example, I also do
x <- setnames(x, x) a lot at the start of my purrr piping sequences and that could have a convenience function.
This I dislike about adding these functions:
- They seem like they could be inefficient applying to each element independently. In the case of the
discard_names example above, the anonymous function would have been called twice, but since %in% is vectorized here a single function call across the whole vector of names would have been fine to get the boolean results of the function. On a list thousands of elements long this could be a problem.
- The set of functions you could theoretically open to having some sort of "name" equivalent (like
map_names()) is so big I do fall into some "slippery slope" fears of this going too far.
These functions seems simple enough that I would think I could personally make a PR request to add them. I would love some feedback on if other people would want them included or if they should be changed somehow. Thank you!!
From the discussion in this twitter thread it seems there is a need to remove elements from lists by name. The current "best" solution is to assign an element NULL using base R commands, which does not have an elegant tidy piping implementation. Since this is a fairly common task that needs to be done, it would be helpful to create a purrr a function that can easily be put within a sequence of piped purrr calls:
One approach I have been thinking of after writing that last tweet would be to create a
purrr::keep_names()andpurrr::discard_names(). The point of these functions would be to closely mimic the existingpurrr::keep()andpurrr::discard(), but to have the functions be applied to the names of the list rather than the values. It could also work on a vectors of names as the input rather than a function, for the common case when you just want to keep/remove specific elements. So something like this:And then in the case of the Twitter thread, Elaine could have just added
discard_names("b")into her code.Things I like about adding these functions:
x <- setnames(x, x)a lot at the start of my purrr piping sequences and that could have a convenience function.This I dislike about adding these functions:
discard_namesexample above, the anonymous function would have been called twice, but since %in% is vectorized here a single function call across the whole vector of names would have been fine to get the boolean results of the function. On a list thousands of elements long this could be a problem.map_names()) is so big I do fall into some "slippery slope" fears of this going too far.These functions seems simple enough that I would think I could personally make a PR request to add them. I would love some feedback on if other people would want them included or if they should be changed somehow. Thank you!!