Open DavisVaughan opened 1 year ago
If I understand correctly, this would solve the following problem right?
I have a list of this format. (duplicate names, but similar structure)
l1 <- list(x = c(el1 = 1), x = c(el2 = 2, el3 = 3), y = c(el1 = 1))
l1
$x
el1
1
$x
el2 el3
2 3
$y
el1
4
I'd like to apply a purrr transformation to change it to
list(x = c(el1 = 1, el2 = 2, el3 = 3), y = c(el1 = 1))
I tried using list_flatten()
, but it insists on keeping l1
structure unchanged.
I may have gotten lost, but couldn't find an example that does this.
I was able to almost solve this with base R unlist()
which ends up giving
unlist(l1)
x.el1 x.el2 x.el3 y.el1
1 2 3 1
but I don't really trust how unlist()
handles pretty much anything in a surprising way..
unlist() is also not mentioned in the vignette https://purrr.tidyverse.org/articles/base.html (which doesn't reflect the 1.0 API exactly) i.e. mentions map_df*()
functions, and doesn't mention newly introduced functions.
What would be a close equivalent of purrr vs unlist() ?
list_c()
almost works, but it would be great if it had a name_spec
argument.
purrr::list_c(l1)
el1 el2 el3 el1
1 2 3 2
Loses the names
Edit: from https://github.com/tidyverse/purrr/pull/998, vctrs::list_unchop()
seems to have a way to deal with names, maybe a xref could be inserted somewhere...
I added this to dplyr, but it probably belongs in purrr and would make for an easier transition path for the deprecated
rlang::flatten_if()
andrlang::squash()
functionsSee https://github.com/r-lib/rlang/pull/1576 and https://github.com/tidyverse/dplyr/pull/6759